DEV Community

Cover image for Introducing Shest: A Test Runner for Bash Scripts
aagamezl
aagamezl

Posted on

2

Introducing Shest: A Test Runner for Bash Scripts

Shest: A Test Runner for Bash Scripts

Shest is an intuitive test runner for Bash scripts, inspired by Node.js Jest. The name Shest stands for [Sh]ell T[est], and it simplifies testing by providing a clear structure for writing, organizing, and executing shell script tests. With built-in support for assertions, Shest empowers developers to write reliable and maintainable Bash scripts effortlessly.

πŸš€ Installation

You can install shest using a single command. The installation script checks if shest is already installed, downloads the latest version if not, and sets it up for global use.

Installation Command:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/aagamezl/shest/master/tools/install.sh)"
Enter fullscreen mode Exit fullscreen mode

What the Command Does:

  1. Checks if shest Exists:

    • Verifies if shest is already present in /home/$USER.
    • Skips installation if already installed.
  2. Downloads the Script:

    • Fetches the latest version from the GitHub repository using curl.
  3. Places the Script in /home/$USER:

    • Saves the script to /home/$USER/shest.sh for global access.
  4. Displays Installation Status:

    • Notifies whether the installation was successful or failed.

πŸ›  Usage

To use shest in your shell scripts, include it using the source command. Here’s an example test suite:

#!/bin/bash

# Include the shest framework
source /home/$USER/shest.sh

# Define a test suite
describe "Example Test Suite" <<'TEST'
  test "Simple equality check" <<'SUITE'
    toBeEqual "1 + 1 equals 2" "$(echo $((1 + 1)))" "2"
  SUITE

  test "String contains check" <<'SUITE'
    toContain "Check substring" "Hello, World!" "World"
  SUITE
TEST

# Run the tests
pluralize_test
Enter fullscreen mode Exit fullscreen mode

πŸ” Syntax Overview

Shest’s syntax is designed to be intuitive:

  1. describe Block: Groups related test cases.
  2. test Block: Defines individual test cases.
  3. Assertions: Built-in functions like toBeEqual, toBeNotEqual, toBeTrue, toBeFalse, and toContain to validate outcomes.
  4. Heredoc Syntax: Encapsulates multi-line strings without interpreting variables or special characters.

Example:

describe "Nouns ending in -ss, -sh, -ch, -x, -o" <<'TEST'
  test "Testing toBeEqual" <<'SUITE'
    toBeEqual "hello" "hello"
    toBeEqual 1 1
  SUITE

  test "Testing toBeNotEqual" <<'SUITE'
    toBeNotEqual "hello" "world"
  SUITE

  test "Testing toBeTrue" <<'SUITE'
    toBeTrue true
  SUITE

  test "Testing toBeFalse" <<'SUITE'
    toBeFalse false
  SUITE

  test "Testing toContain" <<'SUITE'
    toContain "hello world" "world"
    toContain 123 2
  SUITE
TEST
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Test Results

Shest provides a detailed report for each test run:

  • Pass/Fail Marks: Tests are marked as βœ“ (Pass) or βœ— (Fail).
  • Summary: Shows the total number of test suites and individual tests that passed/failed.
  • Time Taken: Displays execution time for all tests.

Example:

β€’ Testing Assertions
  Testing toBeEqual
    βœ“ toBeEqual: Passed
    βœ“ toBeEqual: Passed

  Testing toBeNotEqual
    βœ“ toBeNotEqual: Passed

  Testing toBeTrue
    βœ“ toBeTrue: Passed

  Testing toBeFalse
    βœ“ toBeFalse: Passed

  Testing toContain
    βœ“ toContain: Passed
    βœ“ toContain: Passed

Test Suites:    0 failed, 5 passed, 5 total
Tests:          0 failed, 7 passed, 7 total
Time:           0.01 s
Enter fullscreen mode Exit fullscreen mode

🌟 Why Use Shest?

  • Intuitive Syntax: Simplifies writing and organizing Bash script tests.
  • Comprehensive Assertions: Offers built-in functions to validate various conditions.
  • Improved Productivity: Saves time by identifying issues early.

Start testing your Bash scripts with Shest today and experience the simplicity of reliable shell scripting! πŸš€


Explore Shest on GitHub for more details.

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

Collapse
 
d-x profile image
dx β€’

Another tool to run tests in Bash: github.com/kward/shunit2

Collapse
 
aagamezl profile image
aagamezl β€’

Thanks for the comment, yes, I saw the library while developing my library, have some good features.

I'm planning to add before, beforeEach, after, afterEach, and a better test report. Also, if I can, the line number where an error occurs, but this last is a big maybe.

@d-x can you please try my library and give me some feedback, I would appreciate it.

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay