git-secret/vendor/bats-core/man/bats.7

235 lines
7.7 KiB
Groff
Raw Normal View History

.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
2022-01-17 13:58:05 +00:00
.TH "BATS" "7" "August 2021" "bats-core" "Bash Automated Testing System"
.
.SH "NAME"
\fBbats\fR \- Bats test file format
.
.SH "DESCRIPTION"
A 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\.
.
.IP "" 4
.
.nf
#!/usr/bin/env bats
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" \-eq 4 ]
}
@test "addition using dc" {
result="$(echo 2 2+p | dc)"
[ "$result" \-eq 4 ]
}
.
.fi
.
.IP "" 0
.
.P
Each Bats test file is evaluated n+1 times, where \fIn\fR 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\.
.
.SH "THE RUN HELPER"
2022-01-17 13:58:05 +00:00
Usage: run [OPTIONS] [\-\-] <command\.\.\.> Options: ! check for non zero exit code =\fIN\fR check that exit code is \fIN\fR \-\-output {merged,separate,stdout,stderr} control which output is recorded \-\-keep\-empty\-lines retain emtpy lines in \fB${lines[@]}\fR
.
.P
Many Bats tests need to run a command and then make assertions about its exit status and output\. Bats includes a \fBrun\fR 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, \fBrun\fR returns with a \fB0\fR status code so you can continue to make assertions in your test case\.
.
.P
For example, let\'s say you\'re testing that the \fBfoo\fR command, when passed a nonexistent filename, exits with a \fB1\fR status code and prints an error message\.
.
.IP "" 4
.
.nf
@test "invoking foo with a nonexistent file prints an error" {
2022-01-17 13:58:05 +00:00
run -1 foo nonexistent_filename
[ "$output" = "foo: no such file \'nonexistent_filename\'" ]
}
.
.fi
.
.IP "" 0
.
.P
2022-01-17 13:58:05 +00:00
The \fB=1\fR as first argument tells \fBrun\fR 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:
.
.IP "" 4
.
.nf
(in test file test\.bats, line 2)
`run -1 foo nonexistent_filename\' failed, expected exit code 1, got 127
.
.fi
.
.IP "" 0
.
.P
This error indicates a possible problem with the installation or configuration of \fBfoo\fR; note that a simple \fB[ $status != 0 ]\fR test would not have caught this kind of failure\.
.
.P
The \fB$status\fR variable contains the status code of the command, and the \fB$output\fR variable contains the combined contents of the command\'s standard output and standard error streams\.
.
.P
A third special variable, the \fB$lines\fR array, is available for easily accessing individual lines of output\. For example, if you want to test that invoking \fBfoo\fR without any arguments prints usage information on the first line:
.
.IP "" 4
.
.nf
@test "invoking foo without arguments prints usage" {
2022-01-17 13:58:05 +00:00
run -1 foo
[ "${lines[0]}" = "usage: foo <filename>" ]
}
.
.fi
.
.IP "" 0
.
2022-01-17 13:58:05 +00:00
.P
By default \fBrun\fR leaves out empty lines in \fB${lines[@]}\fR\. Use \fBrun \-\-keep\-empty\-lines\fR to retain them\.
.
.P
Additionally, you can use \fBrun \-\-output <mode>\fR to control what goes into \fB$output\fR and \fB$lines\fR\. The available values for \fB<mode>\fR are:
.
.IP "\(bu" 4
\fBmerged\fR: the default when \fB\-\-output\fR is not specified, interleaves stdout and stderr
.
.IP "\(bu" 4
\fBseparate\fR: splits stderr off to \fB$stderr\fR and \fB${stderr_lines[@]}\fR, stdout is still available as \fB$output\fR and \fB${lines[@]}\fR
.
.IP "\(bu" 4
\fBstderr\fR: discards stdout and fills \'$stderr\fBand\fR${stderr_lines[@]}`
.
.IP "\(bu" 4
\fBstdout\fR: discards stdout and fills \fB$output\fR and \fB${lines[@]}\fR
.
.IP "" 0
.
.P
All additional parameters to run should come before the command\. If you want to run a command that starts with \fB\-\fR, prefix it with \fB\-\-\fR to prevent \fBrun\fR from parsing it as an option\.
.
.SH "THE LOAD COMMAND"
You may want to share common code across multiple test files\. Bats includes a convenient \fBload\fR 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 \fBtest/foo\.bats\fR, the command
.
.IP "" 4
.
.nf
load test_helper
.
.fi
.
.IP "" 0
.
.P
will source the script \fBtest/test_helper\.bash\fR in your test file\. This can be useful for sharing functions to set up your environment or load fixtures\.
.
.SH "THE SKIP COMMAND"
Tests can be skipped by using the \fBskip\fR command at the point in a test you wish to skip\.
.
.IP "" 4
.
.nf
@test "A test I don\'t want to execute for now" {
skip
2022-01-17 13:58:05 +00:00
run -0 foo
}
.
.fi
.
.IP "" 0
.
.P
Optionally, you may include a reason for skipping:
.
.IP "" 4
.
.nf
@test "A test I don\'t want to execute for now" {
skip "This command will return zero soon, but not now"
2022-01-17 13:58:05 +00:00
run -0 foo
}
.
.fi
.
.IP "" 0
.
.P
Or you can skip conditionally:
.
.IP "" 4
.
.nf
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn\'t bar"
fi
2022-01-17 13:58:05 +00:00
run -0 foo
}
.
.fi
.
.IP "" 0
.
.SH "SETUP AND TEARDOWN FUNCTIONS"
You can define special \fBsetup\fR and \fBteardown\fR 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\.
.
.SH "CODE OUTSIDE OF TEST CASES"
You can include code in your test file outside of \fB@test\fR 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 \fB@test\fR, \fBsetup\fR or \fBteardown\fR functions must be redirected to \fBstderr\fR (\fB>&2\fR)\. Otherwise, the output may cause Bats to fail by polluting the TAP stream on \fBstdout\fR\.
.
.SH "SPECIAL VARIABLES"
There are several global variables you can use to introspect on Bats tests:
.
.IP "\(bu" 4
\fB$BATS_TEST_FILENAME\fR is the fully expanded path to the Bats test file\.
.
.IP "\(bu" 4
\fB$BATS_TEST_DIRNAME\fR is the directory in which the Bats test file is located\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NAMES\fR is an array of function names for each test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NAME\fR is the name of the function containing the current test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_DESCRIPTION\fR is the description of the current test case\.
.
.IP "\(bu" 4
\fB$BATS_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test file\.
.
.IP "\(bu" 4
2021-05-03 12:43:04 +00:00
\fB$BATS_SUITE_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test suite (over all files)\.
.
.IP "\(bu" 4
2022-01-17 13:58:05 +00:00
\fB$BATS_TMPDIR\fR is the base temporary directory used by bats to create its temporary files / directories\. (default: \fB$TMPDIR\fR\. If \fB$TMPDIR\fR is not set, \fB/tmp\fR is used\.)
.
.IP "\(bu" 4
\fB$BATS_RUN_TMPDIR\fR is the location to the temporary directory used by bats to store all its internal temporary files during the tests\. (default: \fB$BATS_TMPDIR/bats\-run\-$BATS_ROOT_PID\-XXXXXX\fR)
.
2021-05-03 12:43:04 +00:00
.IP "\(bu" 4
\fB$BATS_FILE_EXTENSION\fR (default: \fBbats\fR) specifies the extension of test files that should be found when running a suite (via \fBbats [\-r] suite_folder/\fR)
.
2022-01-17 13:58:05 +00:00
.IP "\(bu" 4
\fB$BATS_SUITE_TMPDIR\fR is a temporary directory common to all tests of a suite\. Could be used to create files required by multiple tests\.
.
.IP "\(bu" 4
\fB$BATS_FILE_TMPDIR\fR is a temporary directory common to all tests of a test file\. Could be used to create files required by multiple tests in the same test file\.
.
.IP "\(bu" 4
\fB$BATS_TEST_TMPDIR\fR is a temporary directory unique for each test\. Could be used to create files required only for specific tests\.
.
.IP "" 0
.
.SH "SEE ALSO"
\fBbash\fR(1), \fBbats\fR(1)