bats: following the output of a long-running test

Viewed 103

We have a couple of bats tests that take a long time to run and produce output that lets the human operator guess how far the test has progressed.

We'd therefore like the output of these tests to be shown while they're running.

The bats-core documentation topic Printing to the terminal says under "Printing from within a test function":

To have text printed unconditionally from within a test function you need to redirect the output to file descriptor 3, eg echo 'text' >&3. This output will become part of the TAP stream. You are encouraged to prepend text printed this way with a hash (eg echo '# text' >&3) in order to produce 100% TAP compliant output

Summary: Prefer echo '# text' >&3 over echo 'text' >&3

The problem is that echo '# text' >&3 is buffered until the test is finished so the human operator can't follow the progress. Is there any way to get TAP compliant output and see the output as it is being generated?

Example:

@test "sleeper nohash" {
    for i in $(seq 1 3) ; do
        echo "Iteration $i" >&3
        sleep 1
    done
}

@test "sleeper hash" {
    for i in $(seq 1 3) ; do
        echo "# Iteration $i" >&3
        sleep 1
    done
}

bats sleeper.bats shows the non-compliant output from test sleeper nohash as it is being echoed, while sleeper hash's compliant output is shown all at once after the test is finished.

enter image description here

0 Answers
Related