Fish Shell:
Writing tests

How to:

Fish doesn’t have a built-in testing framework like some other programming environments. However, you can write simple test scripts that use assertions to check the behavior of your functions. Additionally, you can leverage third-party tools like fishtape for a more comprehensive testing suite.

Example 1: Basic Test Script

Let’s start with a basic function in Fish that calculates the sum of two numbers:

function add --description 'Add two numbers'
    set -l sum (math $argv[1] + $argv[2])
    echo $sum
end

You can write a basic test script for this function like so:

function test_add
    set -l result (add 3 4)
    if test $result -eq 7
        echo "test_add passed"
    else
        echo "test_add failed"
    end
end

test_add

Running this script would output:

test_add passed

Example 2: Using Fishtape

For a more robust testing solution, you can use fishtape, a TAP-producing test runner for Fish.

First, install fishtape if you haven’t already:

fisher install jorgebucaran/fishtape

Next, create a test file for your add function, e.g., add_test.fish:

test "Adding 3 and 4 yields 7"
    set result (add 3 4)
    echo "$result" | fishtape
end

To run the test, use the following command:

fishtape add_test.fish

Sample output might look like:

TAP version 13
# Adding 3 and 4 yields 7
ok 1 - test_add passed

This tells you that the test passed successfully. fishtape allows you to structure more detailed tests and provides informative output, facilitating easier debugging and comprehensive test coverage for your Fish scripts.