DEV Community

Cover image for Building a Basic Testing Framework in Bash 🐚
Piyush Chauhan
Piyush Chauhan

Posted on

Building a Basic Testing Framework in Bash 🐚

In the world of software development, testing is crucial for ensuring that your code behaves as expected. In this article, we'll explore a simple yet effective Bash script that serves as a basic testing framework. This framework will help you track test results, log outcomes, and summarize the results of your tests. Let's dive in! 🚀

Overview of the Script

The script consists of several components that work together to facilitate testing:

  1. Variables for Tracking Test Results: These variables keep count of the total tests, passed tests, and failed tests.
  2. Logging Function: A function to log the results of each test.
  3. Assertion Functions: Functions to check for equality and command success.
  4. Summary Function: A function to display the overall results after running the tests.

Breakdown of the Script

1. Variables for Tracking Test Results

total_tests=0
passed_tests=0
failed_tests=0
Enter fullscreen mode Exit fullscreen mode

These variables initialize counters for tracking how many tests have been run, how many have passed, and how many have failed.

2. Logging Function

log_result() {
    local status=$1
    local message=$2

    if [ "$status" -eq 0 ]; then
        echo "✅ PASS: $message"
        ((passed_tests++))
    else
        echo "❌ FAIL: $message"
        ((failed_tests++))
    fi
}
Enter fullscreen mode Exit fullscreen mode

The log_result function takes a status code and a message. If the status is 0, it logs a pass; otherwise, it logs a failure and increments the respective counters.

3. Assertion Functions

  • Assert Equality
assert_equal() {
    local expected=$1
    local actual=$2
    local test_name=$3

    ((total_tests++))
    if [ "$expected" == "$actual" ]; then
        log_result 0 "$test_name"
    else
        log_result 1 "$test_name (Expected: '$expected', Got: '$actual')"
    fi
}
Enter fullscreen mode Exit fullscreen mode

This function checks if two values are equal. It logs the result and updates the total test count.

  • Assert Command Success
assert_command_success() {
    local command="$1"
    local test_name=$2

    ((total_tests++))
    eval "$command" &> /dev/null
    if [ $? -eq 0 ]; then
        log_result 0 "$test_name"
    else
        log_result 1 "$test_name (Command failed)"
    fi
}
Enter fullscreen mode Exit fullscreen mode

This function runs a command and checks its exit status. If the command succeeds, it logs a pass; otherwise, it logs a failure.

4. Summary Function

summary() {
    echo
    echo "Total Tests: $total_tests"
    echo "Passed: $passed_tests"
    echo "Failed: $failed_tests"

    if [ "$failed_tests" -eq 0 ]; then
        echo "🎉 All tests passed!"
    else
        echo "⚠️ Some tests failed."
    fi
}
Enter fullscreen mode Exit fullscreen mode

After all tests are executed, this function summarizes the results, indicating how many tests passed or failed.

Example Usage

Below is an example of how to use this testing framework:

echo "Running Tests..."
assert_equal "hello" "hello" "Check if strings are equal"
assert_equal "1" "2" "Check if integers are equal"
assert_command_success "ls /" "Check if 'ls /' command succeeds"
assert_command_success "invalid_command" "Check if invalid command fails"

# Display the summary
summary
Enter fullscreen mode Exit fullscreen mode

In this example:

  • We check string equality.
  • We validate integer equality (which will fail).
  • We run a valid command (ls /) and an invalid command (invalid_command).

Conclusion 🎊

This basic testing framework in Bash provides a simple way to automate your testing process. By using functions to assert conditions and log results, you can easily track the health of your scripts. Feel free to expand upon this framework by adding more complex assertions or incorporating additional features as needed!

Happy scripting! 🖥️✨

Top comments (0)