|
NAMEbats - Bats test file formatDESCRIPTIONA Bats test file is a Bash script with special syntax for defining test cases. Under the hood, each test case is just a function with a description.
Each Bats test file is evaluated n+1 times, where n is the number of test cases in the file. The first run counts the number of test cases, then iterates over the test cases and executes each one in its own process. THE RUN HELPERUsage: run [OPTIONS] [--] <command...> Options: ! check for non zero exit code -N check that exit code is N --separate-stderr split stderr and stdout --keep-empty-lines retain emtpy lines in ${lines[@]}/${stderr_lines[@]}Many Bats tests need to run a command and then make assertions about its exit status and output. Bats includes a run helper that invokes its arguments as a command, saves the exit status and output into special global variables, and (optionally) checks exit status against a given expected value. If successful, run returns with a 0 status code so you can continue to make assertions in your test case. For example, let´s say you´re testing that the foo command, when passed a nonexistent filename, exits with a 1 status code and prints an error message.
The -1 as first argument tells run to expect 1 as an exit status, and to fail if the command exits with any other value. On failure, both actual and expected values will be displayed, along with the invoked command and its output:
This error indicates a possible problem with the installation or configuration of foo; note that a simple [ $status != 0 ] test would not have caught this kind of failure. The $status variable contains the status code of the command, and the $output variable contains the combined contents of the command´s standard output and standard error streams. A third special variable, the $lines array, is available for easily accessing individual lines of output. For example, if you want to test that invoking foo without any arguments prints usage information on the first line:
By default run leaves out empty lines in ${lines[@]}. Use run --keep-empty-lines to retain them. Additionally, you can use --separate-stderr to split stdout and stderr into $output/$stderr and ${lines[@]}/${stderr_lines[@]}. All additional parameters to run should come before the command. If you want to run a command that starts with -, prefix it with -- to prevent run from parsing it as an option. THE LOAD COMMANDYou may want to share common code across multiple test files. Bats includes a convenient load command for sourcing a Bash source file relative to the location of the current test file. For example, if you have a Bats test in test/foo.bats, the command
will source the script test/test_helper.bash in your test file. This can be useful for sharing functions to set up your environment or load fixtures. THE SKIP COMMANDTests can be skipped by using the skip command at the point in a test you wish to skip.
Optionally, you may include a reason for skipping:
Or you can skip conditionally:
SETUP AND TEARDOWN FUNCTIONSYou can define special setup and teardown functions which run before and after each test case, respectively. Use these to load fixtures, set up your environment, and clean up when you´re done.CODE OUTSIDE OF TEST CASESYou can include code in your test file outside of @test functions. For example, this may be useful if you want to check for dependencies and fail immediately if they´re not present. However, any output that you print in code outside of @test, setup or teardown functions must be redirected to stderr (>&2). Otherwise, the output may cause Bats to fail by polluting the TAP stream on stdout.SPECIAL VARIABLESThere are several global variables you can use to introspect on Bats tests:
SEE ALSObash(1), bats(1)
Visit the GSP FreeBSD Man Page Interface. |