tests
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# Makefile
|
||||
#
|
||||
# bats-core manpages
|
||||
#
|
||||
RONN := ronn -W
|
||||
PAGES := bats.1 bats.7
|
||||
ORG := bats-core
|
||||
MANUAL := 'Bash Automated Testing System'
|
||||
ISOFMT := $(shell date -I)
|
||||
RM := rm -f
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: $(PAGES)
|
||||
|
||||
bats.1: bats.1.ronn
|
||||
$(RONN) --date=$(ISOFMT) --manual=$(MANUAL) --organization=$(ORG) --roff $<
|
||||
|
||||
bats.7: bats.7.ronn
|
||||
$(RONN) --date=$(ISOFMT) --manual=$(MANUAL) --organization=$(ORG) --roff $<
|
||||
|
||||
clean:
|
||||
$(RM) $(PAGES)
|
||||
@@ -0,0 +1,5 @@
|
||||
Bats man pages are generated with [Ronn](http://rtomayko.github.io/ronn/).
|
||||
|
||||
After making changes to `bats.1.ronn` or `bats.7.ronn`, run `make` in
|
||||
this directory to generate `bats.1` and `bats.7`. **Do not edit the
|
||||
`bats.1` or `bats.7` files directly.**
|
||||
@@ -0,0 +1,143 @@
|
||||
.\" generated with Ronn-NG/v0.9.1
|
||||
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
|
||||
.TH "BATS" "1" "November 2022" "bats-core" "Bash Automated Testing System"
|
||||
.SH "NAME"
|
||||
\fBbats\fR \- Bash Automated Testing System
|
||||
.SH "SYNOPSIS"
|
||||
Usage: bats [OPTIONS] \fItests\fR bats [\-h | \-v]
|
||||
.P
|
||||
\fItests\fR is the path to a Bats test file, or the path to a directory containing Bats test files (ending with "\.bats")
|
||||
.SH "DESCRIPTION"
|
||||
Bats is a TAP\-compliant testing framework for Bash\. It provides a simple way to verify that the UNIX programs you write behave as expected\.
|
||||
.P
|
||||
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\.
|
||||
.P
|
||||
Test cases consist of standard shell commands\. Bats makes use of Bash\'s \fBerrexit\fR (\fBset \-e\fR) option when running test cases\. If every command in the test case exits with a \fB0\fR status code (success), the test passes\. In this way, each line is an assertion of truth\.
|
||||
.P
|
||||
See \fBbats\fR(7) for more information on writing Bats tests\.
|
||||
.SH "RUNNING TESTS"
|
||||
To run your tests, invoke the \fBbats\fR interpreter with a path to a test file\. The file\'s test cases are run sequentially and in isolation\. If all the test cases pass, \fBbats\fR exits with a \fB0\fR status code\. If there are any failures, \fBbats\fR exits with a \fB1\fR status code\.
|
||||
.P
|
||||
You can invoke the \fBbats\fR interpreter with multiple test file arguments, or with a path to a directory containing multiple \fB\.bats\fR files\. Bats will run each test file individually and aggregate the results\. If any test case fails, \fBbats\fR exits with a \fB1\fR status code\.
|
||||
.SH "FILTERING TESTS"
|
||||
There are multiple mechanisms to filter which tests to execute:
|
||||
.IP "\[ci]" 4
|
||||
\fB\-\-filter <regex>\fR to filter by test name
|
||||
.IP "\[ci]" 4
|
||||
\fB\-\-filter\-status <status>\fR to filter by the test\'s status in the last run
|
||||
.IP "\[ci]" 4
|
||||
\fB\-\-filter\-tags <tag\-list>\fR to filter by the tags of a test
|
||||
.IP "" 0
|
||||
.SH "\-\-FILTER\-TAGS <var>TAG\-LIST</var>"
|
||||
Tags can be used for finegrained filtering of which tests to run via \fB\-\-filter\-tags\fR\. This accepts a comma separated list of tags\. Only tests that match all of these tags will be executed\. For example, \fBbats \-\-filter\-tags a,b,c\fR will pick up tests with tags \fBa,b,c\fR, but not tests that miss one or more of those tags\.
|
||||
.P
|
||||
Additionally, you can specify negative tags via \fBbats \-\-filter\-tags a,!b,c\fR, which now won\'t match tests with tags \fBa,b,c\fR, due to the \fBb\fR, but will select \fBa,c\fR\. To put it more formally, \fB\-\-filter\-tags\fR is a boolean conjunction\.
|
||||
.P
|
||||
To allow for more complex queries, you can specify multiple \fB\-\-filter\-tags\fR\. A test will be executed, if it matches at least one of them\. This means multiple \fB\-\-filter\-tags\fR form a boolean disjunction\.
|
||||
.P
|
||||
A query of \fB\-\-filter\-tags a,!b \-\-filter\-tags b,c\fR can be translated to: Execute only tests that (have tag a, but not tag b) or (have tag b and c)\.
|
||||
.P
|
||||
An empty tag list matches tests without tags\.
|
||||
.SH "OPTIONS"
|
||||
.TP
|
||||
\fB\-c\fR, \fB\-\-count\fR
|
||||
Count the number of test cases without running any tests
|
||||
.TP
|
||||
\fB\-\-code\-quote\-style <style>\fR
|
||||
A two character string of code quote delimiters or \fBcustom\fR which requires setting \fB$BATS_BEGIN_CODE_QUOTE\fR and \fB$BATS_END_CODE_QUOTE\fR\. Can also be set via \fB$BATS_CODE_QUOTE_STYLE\fR\.
|
||||
.TP
|
||||
\fB\-f\fR, \fB\-\-filter <regex>\fR
|
||||
Filter test cases by names matching the regular expression
|
||||
.TP
|
||||
\fB\-F\fR, \fB\-\-formatter <type>\fR
|
||||
Switch between formatters: pretty (default), tap (default w/o term), tap13, junit, \fB/<absolute path to formatter>\fR
|
||||
.TP
|
||||
\fB\-\-filter\-status <status>\fR
|
||||
Only run tests with the given status in the last completed (no CTRL+C/SIGINT) run\. Valid \fIstatus\fR values are: failed \- runs tests that failed or were not present in the last run missed \- runs tests that were not present in the last run
|
||||
.TP
|
||||
\fB\-\-filter\-tags <comma\-separated\-tag\-list>\fR
|
||||
Only run tests that match all the tags in the list (\fB&&\fR)\. You can negate a tag via prepending \fB!\fR\. Specifying this flag multiple times allows for logical or (\fB||\fR): \fB\-\-filter\-tags A,B \-\-filter\-tags A,!C\fR matches tags \fB(A && B) || (A && !C)\fR
|
||||
.TP
|
||||
\fB\-\-gather\-test\-outputs\-in <directory>\fR
|
||||
Gather the output of failing \fIand\fR passing tests as files in directory
|
||||
.TP
|
||||
\fB\-h\fR, \fB\-\-help\fR
|
||||
Display this help message
|
||||
.TP
|
||||
\fB\-j\fR, \fB\-\-jobs <jobs>\fR
|
||||
Number of parallel jobs (requires GNU parallel)
|
||||
.TP
|
||||
\fB\-\-no\-tempdir\-cleanup\fR
|
||||
Preserve test output temporary directory
|
||||
.TP
|
||||
\fB\-\-no\-parallelize\-across\-files\fR
|
||||
Serialize test file execution instead of running them in parallel (requires \-\-jobs >1)
|
||||
.TP
|
||||
\fB\-\-no\-parallelize\-within\-files\fR
|
||||
Serialize test execution within files instead of running them in parallel (requires \-\-jobs >1)
|
||||
.TP
|
||||
\fB\-\-report\-formatter <type>\fR
|
||||
Switch between reporters (same options as \-\-formatter)
|
||||
.TP
|
||||
\fB\-o\fR, \fB\-\-output <dir>\fR
|
||||
Directory to write report files
|
||||
.TP
|
||||
\fB\-p\fR, \fB\-\-pretty\fR
|
||||
Shorthand for "\-\-formatter pretty"
|
||||
.TP
|
||||
\fB\-\-print\-output\-on\-failure\fR
|
||||
Automatically print the value of \fB$output\fR on failed tests
|
||||
.TP
|
||||
\fB\-r\fR, \fB\-\-recursive\fR
|
||||
Include tests in subdirectories
|
||||
.TP
|
||||
\fB\-\-show\-output\-of\-passing\-tests\fR
|
||||
Print output of passing tests
|
||||
.TP
|
||||
\fB\-t\fR, \fB\-\-tap\fR
|
||||
Shorthand for "\-\-formatter tap"
|
||||
.TP
|
||||
\fB\-T\fR, \fB\-\-timing\fR
|
||||
Add timing information to tests
|
||||
.TP
|
||||
\fB\-x\fR, \fB\-\-trace\fR
|
||||
Print test commands as they are executed (like \fBset \-x\fR)
|
||||
.TP
|
||||
\fB\-\-verbose\-run\fR
|
||||
Make \fBrun\fR print \fB$output\fR by default
|
||||
.TP
|
||||
\fB\-v\fR, \fB\-\-version\fR
|
||||
Display the version number
|
||||
.SH "OUTPUT"
|
||||
When you run Bats from a terminal, you\'ll see output as each test is performed, with a check\-mark next to the test\'s name if it passes or an "X" if it fails\.
|
||||
.IP "" 4
|
||||
.nf
|
||||
$ bats addition\.bats
|
||||
✓ addition using bc
|
||||
✓ addition using dc
|
||||
|
||||
2 tests, 0 failures
|
||||
.fi
|
||||
.IP "" 0
|
||||
.P
|
||||
If Bats is not connected to a terminal\-\-in other words, if you run it from a continuous integration system or redirect its output to a file\-\-the results are displayed in human\-readable, machine\-parsable TAP format\. You can force TAP output from a terminal by invoking Bats with the \fB\-\-tap\fR option\.
|
||||
.IP "" 4
|
||||
.nf
|
||||
$ bats \-\-tap addition\.bats
|
||||
1\.\.2
|
||||
ok 1 addition using bc
|
||||
ok 2 addition using dc
|
||||
.fi
|
||||
.IP "" 0
|
||||
.SH "EXIT STATUS"
|
||||
The \fBbats\fR interpreter exits with a value of \fB0\fR if all test cases pass, or \fB1\fR if one or more test cases fail\.
|
||||
.SH "SEE ALSO"
|
||||
Bats wiki: \fIhttps://github\.com/bats\-core/bats\-core/wiki/\fR
|
||||
.P
|
||||
\fBbash\fR(1), \fBbats\fR(7)
|
||||
.SH "COPYRIGHT"
|
||||
(c) 2017\-2022 bats\-core organization
|
||||
.br
|
||||
(c) 2011\-2016 Sam Stephenson
|
||||
.P
|
||||
Bats is released under the terms of an MIT\-style license\.
|
||||
@@ -0,0 +1,192 @@
|
||||
bats(1) -- Bash Automated Testing System
|
||||
========================================
|
||||
|
||||
|
||||
SYNOPSIS
|
||||
--------
|
||||
|
||||
Usage: bats [OPTIONS] <tests>
|
||||
bats [-h | -v]
|
||||
|
||||
<tests> is the path to a Bats test file, or the path to a directory
|
||||
containing Bats test files (ending with ".bats")
|
||||
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
|
||||
Bats is a TAP-compliant testing framework for Bash. It provides a simple
|
||||
way to verify that the UNIX programs you write behave as expected.
|
||||
|
||||
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.
|
||||
|
||||
Test cases consist of standard shell commands. Bats makes use of
|
||||
Bash's `errexit` (`set -e`) option when running test cases. If every
|
||||
command in the test case exits with a `0` status code (success), the
|
||||
test passes. In this way, each line is an assertion of truth.
|
||||
|
||||
See `bats`(7) for more information on writing Bats tests.
|
||||
|
||||
|
||||
RUNNING TESTS
|
||||
-------------
|
||||
|
||||
To run your tests, invoke the `bats` interpreter with a path to a test
|
||||
file. The file's test cases are run sequentially and in isolation. If
|
||||
all the test cases pass, `bats` exits with a `0` status code. If there
|
||||
are any failures, `bats` exits with a `1` status code.
|
||||
|
||||
You can invoke the `bats` interpreter with multiple test file arguments,
|
||||
or with a path to a directory containing multiple `.bats` files. Bats
|
||||
will run each test file individually and aggregate the results. If any
|
||||
test case fails, `bats` exits with a `1` status code.
|
||||
|
||||
FILTERING TESTS
|
||||
---------------
|
||||
|
||||
There are multiple mechanisms to filter which tests to execute:
|
||||
|
||||
* `--filter <regex>` to filter by test name
|
||||
* `--filter-status <status>` to filter by the test's status in the last run
|
||||
* `--filter-tags <tag-list>` to filter by the tags of a test
|
||||
|
||||
--FILTER-TAGS <TAG-LIST>
|
||||
------------------------
|
||||
|
||||
Tags can be used for finegrained filtering of which tests to run via `--filter-tags`.
|
||||
This accepts a comma separated list of tags. Only tests that match all of these
|
||||
tags will be executed. For example, `bats --filter-tags a,b,c` will pick up tests
|
||||
with tags `a,b,c`, but not tests that miss one or more of those tags.
|
||||
|
||||
Additionally, you can specify negative tags via `bats --filter-tags a,!b,c`,
|
||||
which now won't match tests with tags `a,b,c`, due to the `b`, but will select `a,c`.
|
||||
To put it more formally, `--filter-tags` is a boolean conjunction.
|
||||
|
||||
To allow for more complex queries, you can specify multiple `--filter-tags`.
|
||||
A test will be executed, if it matches at least one of them.
|
||||
This means multiple `--filter-tags` form a boolean disjunction.
|
||||
|
||||
A query of `--filter-tags a,!b --filter-tags b,c` can be translated to:
|
||||
Execute only tests that (have tag a, but not tag b) or (have tag b and c).
|
||||
|
||||
An empty tag list matches tests without tags.
|
||||
|
||||
OPTIONS
|
||||
-------
|
||||
|
||||
* `-c`, `--count`:
|
||||
Count the number of test cases without running any tests
|
||||
* `--code-quote-style <style>`:
|
||||
A two character string of code quote delimiters or `custom`
|
||||
which requires setting `$BATS_BEGIN_CODE_QUOTE` and
|
||||
`$BATS_END_CODE_QUOTE`.
|
||||
Can also be set via `$BATS_CODE_QUOTE_STYLE`.
|
||||
* `--line-reference-format`
|
||||
Controls how file/line references e.g. in stack traces are printed:
|
||||
- comma_line (default): a.bats, line 1
|
||||
- colon: a.bats:1
|
||||
- uri: file:///tests/a.bats:1
|
||||
- custom: provide your own via defining bats_format_file_line_reference_custom
|
||||
with parameters <filename> <line>, store via `printf -v "$output"`
|
||||
* `-f`, `--filter <regex>`:
|
||||
Filter test cases by names matching the regular expression
|
||||
* `-F`, `--formatter <type>`:
|
||||
Switch between formatters: pretty (default), tap (default w/o term), tap13, junit,
|
||||
`/<absolute path to formatter>`
|
||||
* `--filter-status <status>`:
|
||||
Only run tests with the given status in the last completed (no CTRL+C/SIGINT) run.
|
||||
Valid <status> values are:
|
||||
failed - runs tests that failed or were not present in the last run
|
||||
missed - runs tests that were not present in the last run
|
||||
* `--filter-tags <comma-separated-tag-list>`:
|
||||
Only run tests that match all the tags in the list (`&&`). You can negate a
|
||||
tag via prepending `!`.
|
||||
Specifying this flag multiple times allows for logical or (`||`):
|
||||
`--filter-tags A,B --filter-tags A,!C` matches tags `(A && B) || (A && !C)`
|
||||
* `--gather-test-outputs-in <directory>`:
|
||||
Gather the output of failing *and* passing tests as files in directory
|
||||
* `-h`, `--help`:
|
||||
Display this help message
|
||||
* `-j`, `--jobs <jobs>`:
|
||||
Number of parallel jobs (requires GNU parallel)
|
||||
* `--no-tempdir-cleanup`:
|
||||
Preserve test output temporary directory
|
||||
* `--no-parallelize-across-files`:
|
||||
Serialize test file execution instead of running them in parallel (requires --jobs >1)
|
||||
* `--no-parallelize-within-files`:
|
||||
Serialize test execution within files instead of running them in parallel (requires --jobs >1)
|
||||
* `--report-formatter <type>`:
|
||||
Switch between reporters (same options as --formatter)
|
||||
* `-o`, `--output <dir>`:
|
||||
Directory to write report files
|
||||
* `-p`, `--pretty`:
|
||||
Shorthand for "--formatter pretty"
|
||||
* `--print-output-on-failure`:
|
||||
Automatically print the value of `$output` on failed tests
|
||||
* `-r`, `--recursive`:
|
||||
Include tests in subdirectories
|
||||
* `--show-output-of-passing-tests`:
|
||||
Print output of passing tests
|
||||
* `-t`, `--tap`:
|
||||
Shorthand for "--formatter tap"
|
||||
* `-T`, `--timing`:
|
||||
Add timing information to tests
|
||||
* `-x`, `--trace`:
|
||||
Print test commands as they are executed (like `set -x`)
|
||||
* `--verbose-run`:
|
||||
Make `run` print `$output` by default
|
||||
* `-v`, `--version`:
|
||||
Display the version number
|
||||
|
||||
OUTPUT
|
||||
------
|
||||
|
||||
When you run Bats from a terminal, you'll see output as each test is
|
||||
performed, with a check-mark next to the test's name if it passes or
|
||||
an "X" if it fails.
|
||||
|
||||
$ bats addition.bats
|
||||
✓ addition using bc
|
||||
✓ addition using dc
|
||||
|
||||
2 tests, 0 failures
|
||||
|
||||
If Bats is not connected to a terminal--in other words, if you run it
|
||||
from a continuous integration system or redirect its output to a
|
||||
file--the results are displayed in human-readable, machine-parsable
|
||||
TAP format. You can force TAP output from a terminal by invoking Bats
|
||||
with the `--tap` option.
|
||||
|
||||
$ bats --tap addition.bats
|
||||
1..2
|
||||
ok 1 addition using bc
|
||||
ok 2 addition using dc
|
||||
|
||||
|
||||
EXIT STATUS
|
||||
-----------
|
||||
|
||||
The `bats` interpreter exits with a value of `0` if all test cases pass,
|
||||
or `1` if one or more test cases fail.
|
||||
|
||||
|
||||
SEE ALSO
|
||||
--------
|
||||
|
||||
Bats wiki: _https://github.com/bats\-core/bats\-core/wiki/_
|
||||
|
||||
`bash`(1), `bats`(7)
|
||||
|
||||
|
||||
COPYRIGHT
|
||||
---------
|
||||
|
||||
(c) 2017-2022 bats-core organization<br/>
|
||||
(c) 2011-2016 Sam Stephenson
|
||||
|
||||
Bats is released under the terms of an MIT-style license.
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
.\" generated with Ronn-NG/v0.9.1
|
||||
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
|
||||
.TH "BATS" "7" "November 2022" "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 "Tagging tests"
|
||||
Each test has a list of tags attached to it\. Without specification, this list is empty\. Tags can be defined in two ways\. The first being \fB# bats test_tags=\fR:
|
||||
.P
|
||||
# bats test_tags=tag:1, tag:2, tag:3 @test "second test" { # \|\.\|\.\|\. }
|
||||
.P
|
||||
@test "second test" { # \|\.\|\.\|\. }
|
||||
.P
|
||||
These tags (\fBtag:1\fR, \fBtag:2\fR, \fBtag:3\fR) will be attached to the test \fBfirst test\fR\. The second test will have no tags attached\. Values defined in the \fB# bats test_tags=\fR directive will be assigned to the next \fB@test\fR that is being encountered in the file and forgotten after that\. Only the value of the last \fB# bats test_tags=\fR directive before a given test will be used\.
|
||||
.P
|
||||
Sometimes, we want to give all tests in a file a set of the same tags\. This can be achieved via \fB# bats file_tags=\fR\. They will be added to all tests in the file after that directive\. An additional \fB# bats file_tags=\fR directive will override the previously defined values:
|
||||
.IP "" 4
|
||||
.nf
|
||||
@test "Zeroth test" {
|
||||
# will have no tags
|
||||
}
|
||||
|
||||
# bats file_tags=a:b
|
||||
# bats test_tags=c:d
|
||||
|
||||
@test "First test" {
|
||||
# will be tagged a:b, c:d
|
||||
}
|
||||
|
||||
# bats file_tags=
|
||||
|
||||
@test "Second test" {
|
||||
# will have no tags
|
||||
}
|
||||
.fi
|
||||
.IP "" 0
|
||||
.P
|
||||
Tags are case sensitive and must only consist of alphanumeric characters and \fB_\fR, \fB\-\fR, or \fB:\fR\. They must not contain whitespaces! The colon is intended as a separator for (recursive) namespacing\.
|
||||
.P
|
||||
Tag lists must be separated by commas and are allowed to contain whitespace\. They must not contain empty tags like \fBtest_tags=,b\fR (first tag is empty), \fBtest_tags=a,,c\fR, \fBtest_tags=a, ,c\fR (second tag is only whitespace/empty), \fBtest_tags=a,b,\fR (third tag is empty)\.
|
||||
.P
|
||||
Every tag starting with \fBbats:\fR (case insensitive!) is reserved for Bats\' internal use:
|
||||
.TP
|
||||
\fBbats:focus\fR
|
||||
If any test with the tag \fBbats:focus\fR is encountered in a test suite, only those tagged with this tag will be executed\. To prevent the CI from silently running on a subset of tests due to an accidentally committed \fBbats:focus\fR tag, the exit code of successful runs will be overridden to 1\.
|
||||
.IP
|
||||
Should you require the true exit code, e\.g\. for a \fBgit bisect\fR operation, you can disable this behavior by setting \fBBATS_NO_FAIL_FOCUS_RUN=1\fR when running \fBbats\fR, but make sure to not commit this to CI!
|
||||
.SH "THE RUN HELPER"
|
||||
Usage: run [OPTIONS] [\-\-]
|
||||
.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" {
|
||||
run \-1 foo nonexistent_filename
|
||||
[ "$output" = "foo: no such file \'nonexistent_filename\'" ]
|
||||
}
|
||||
.fi
|
||||
.IP "" 0
|
||||
.P
|
||||
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" {
|
||||
run \-1 foo
|
||||
[ "${lines[0]}" = "usage: foo <filename>" ]
|
||||
}
|
||||
.fi
|
||||
.IP "" 0
|
||||
.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 \fB\-\-separate\-stderr\fR to split stdout and stderr into \fB$output\fR/\fB$stderr\fR and \fB${lines[@]}\fR/\fB${stderr_lines[@]}\fR\.
|
||||
.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 BATS_LOAD_LIBRARY COMMAND"
|
||||
Some libraries are installed on the system, e\.g\. by \fBnpm\fR or \fBbrew\fR\. These should not be \fBload\fRed, as their path depends on the installation method\. Instead, one should use \fBbats_load_library\fR together with setting \fBBATS_LIB_PATH\fR, a \fBPATH\fR\-like colon\-delimited variable\.
|
||||
.P
|
||||
\fBbats_load_library\fR has two modes of resolving requests:
|
||||
.IP "1." 4
|
||||
by relative path from the \fBBATS_LIB_PATH\fR to a file in the library
|
||||
.IP "2." 4
|
||||
by library name, expecting libraries to have a \fBload\.bash\fR entrypoint
|
||||
.IP "" 0
|
||||
.P
|
||||
For example if your \fBBATS_LIB_PATH\fR is set to \fB~/\.bats/libs:/usr/lib/bats\fR, then \fBbats_load_library test_helper\fR would look for existing files with the following paths:
|
||||
.IP "\[ci]" 4
|
||||
\fB~/\.bats/libs/test_helper\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB~/\.bats/libs/test_helper/load\.bash\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB/usr/lib/bats/test_helper\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB/usr/lib/bats/test_helper/load\.bash\fR
|
||||
.IP "" 0
|
||||
.P
|
||||
The first existing file in this list will be sourced\.
|
||||
.P
|
||||
If you want to load only part of a library or the entry point is not named \fBload\.bash\fR, you have to include it in the argument: \fBbats_load_library library_name/file_to_load\fR will try
|
||||
.IP "\[ci]" 4
|
||||
\fB~/\.bats/libs/library_name/file_to_load\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB~/\.bats/libs/library_name/file_to_load/load\.bash\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB/usr/lib/bats/library_name/file_to_load\fR
|
||||
.IP "\[ci]" 4
|
||||
\fB/usr/lib/bats/library_name/file_to_load/load\.bash\fR
|
||||
.IP "" 0
|
||||
.P
|
||||
Apart from the changed lookup rules, \fBbats_load_library\fR behaves like \fBload\fR\.
|
||||
.P
|
||||
\fBNote\fR: As seen above \fBload\.bash\fR is the entry point for libraries and meant to load more files from its directory or other libraries\.
|
||||
.P
|
||||
\fBNote\fR: Obviously, the actual \fBBATS_LIB_PATH\fR is highly dependent on the environment\. To maintain a uniform location across systems, (distribution) package maintainers are encouraged to use \fB/usr/lib/bats/\fR as the install path for libraries where possible\. However, if the package manager has another preferred location, like \fBnpm\fR or \fBbrew\fR, you should use this instead\.
|
||||
.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
|
||||
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"
|
||||
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
|
||||
|
||||
run \-0 foo
|
||||
}
|
||||
.fi
|
||||
.IP "" 0
|
||||
.SH "THE BATS_REQUIRE_MINIMUM_VERSION COMMAND"
|
||||
Code for newer versions of Bats can be incompatible with older versions\. In the best case this will lead to an error message and a failed test suite\. In the worst case, the tests will pass erroneously, potentially masking a failure\.
|
||||
.P
|
||||
Use \fBbats_require_minimum_version <Bats version number>\fR to avoid this\. It communicates in a concise manner, that you intend the following code to be run under the given Bats version or higher\.
|
||||
.P
|
||||
Additionally, this function will communicate the current Bats version floor to subsequent code, allowing e\.g\. Bats\' internal warning to give more informed warnings\.
|
||||
.P
|
||||
\fBNote\fR: By default, calling \fBbats_require_minimum_version\fR with versions before Bats 1\.7\.0 will fail regardless of the required version as the function is not available\. However, you can use the bats\-backports plugin (https://github\.com/bats\-core/bats\-backports) to make your code usable with older versions, e\.g\. during migration while your CI system is not yet upgraded\.
|
||||
.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 "\[ci]" 4
|
||||
\fB$BATS_TEST_FILENAME\fR is the fully expanded path to the Bats test file\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_TEST_DIRNAME\fR is the directory in which the Bats test file is located\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_TEST_NAMES\fR is an array of function names for each test case\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_TEST_NAME\fR is the name of the function containing the current test case\.
|
||||
.IP "\[ci]" 4
|
||||
\fBBATS_TEST_NAME_PREFIX\fR will be prepended to the description of each test on stdout and in reports\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_TEST_DESCRIPTION\fR is the description of the current test case\.
|
||||
.IP "\[ci]" 4
|
||||
\fBBATS_TEST_RETRIES\fR is the maximum number of additional attempts that will be made on a failed test before it is finally considered failed\. The default of 0 means the test must pass on the first attempt\.
|
||||
.IP "\[ci]" 4
|
||||
\fBBATS_TEST_TIMEOUT\fR is the number of seconds after which a test (including setup) will be aborted and marked as failed\. Updates to this value in \fBsetup()\fR or \fB@test\fR cannot change the running timeout countdown, so the latest useful update location is \fBsetup_file()\fR\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test file\.
|
||||
.IP "\[ci]" 4
|
||||
\fB$BATS_SUITE_TEST_NUMBER\fR is the (1\-based) index of the current test case in the test suite (over all files)\.
|
||||
.IP "\[ci]" 4
|
||||
\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 "\[ci]" 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)
|
||||
.IP "\[ci]" 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)
|
||||
.IP "\[ci]" 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 "\[ci]" 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 "\[ci]" 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 "\[ci]" 4
|
||||
\fB$BATS_VERSION\fR is the version of Bats running the test\.
|
||||
.IP "" 0
|
||||
.SH "SEE ALSO"
|
||||
\fBbash\fR(1), \fBbats\fR(1)
|
||||
@@ -0,0 +1,404 @@
|
||||
bats(7) -- Bats test file format
|
||||
================================
|
||||
|
||||
|
||||
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.
|
||||
|
||||
#!/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 ]
|
||||
}
|
||||
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Tagging tests
|
||||
-------------
|
||||
|
||||
Each test has a list of tags attached to it. Without specification, this list is empty.
|
||||
Tags can be defined in two ways. The first being `# bats test_tags=`:
|
||||
|
||||
# bats test_tags=tag:1, tag:2, tag:3
|
||||
@test "second test" {
|
||||
# ...
|
||||
}
|
||||
|
||||
@test "second test" {
|
||||
# ...
|
||||
}
|
||||
|
||||
These tags (`tag:1`, `tag:2`, `tag:3`) will be attached to the test `first test`.
|
||||
The second test will have no tags attached. Values defined in the `# bats test_tags=`
|
||||
directive will be assigned to the next `@test` that is being encountered in the
|
||||
file and forgotten after that. Only the value of the last `# bats test_tags=` directive
|
||||
before a given test will be used.
|
||||
|
||||
Sometimes, we want to give all tests in a file a set of the same tags. This can
|
||||
be achieved via `# bats file_tags=`. They will be added to all tests in the file
|
||||
after that directive. An additional `# bats file_tags=` directive will override
|
||||
the previously defined values:
|
||||
|
||||
@test "Zeroth test" {
|
||||
# will have no tags
|
||||
}
|
||||
|
||||
# bats file_tags=a:b
|
||||
# bats test_tags=c:d
|
||||
|
||||
@test "First test" {
|
||||
# will be tagged a:b, c:d
|
||||
}
|
||||
|
||||
# bats file_tags=
|
||||
|
||||
@test "Second test" {
|
||||
# will have no tags
|
||||
}
|
||||
|
||||
Tags are case sensitive and must only consist of alphanumeric characters and `_`,
|
||||
`-`, or `:`. They must not contain whitespaces!
|
||||
The colon is intended as a separator for (recursive) namespacing.
|
||||
|
||||
Tag lists must be separated by commas and are allowed to contain whitespace.
|
||||
They must not contain empty tags like `test_tags=,b` (first tag is empty),
|
||||
`test_tags=a,,c`, `test_tags=a, ,c` (second tag is only whitespace/empty),
|
||||
`test_tags=a,b,` (third tag is empty).
|
||||
|
||||
Every tag starting with `bats:` (case insensitive!) is reserved for Bats'
|
||||
internal use:
|
||||
|
||||
* `bats:focus`:
|
||||
If any test with the tag `bats:focus` is encountered in a test suite, only those tagged with this tag will be executed.
|
||||
|
||||
In focus mode, the exit code of successful runs will be overridden to 1 to prevent CI from silently running on a subset
|
||||
of tests due to an accidentally committed `bats:focus` tag.
|
||||
Should you require the true exit code, e.g. for a `git bisect` operation, you can disable this behavior by setting
|
||||
`BATS_NO_FAIL_FOCUS_RUN=1` when running `bats`, but make sure not to commit this to CI!
|
||||
|
||||
THE RUN HELPER
|
||||
--------------
|
||||
|
||||
Usage: 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 empty 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.
|
||||
|
||||
@test "invoking foo with a nonexistent file prints an error" {
|
||||
run -1 foo nonexistent_filename
|
||||
[ "$output" = "foo: no such file 'nonexistent_filename'" ]
|
||||
}
|
||||
|
||||
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:
|
||||
|
||||
(in test file test.bats, line 2)
|
||||
`run -1 foo nonexistent_filename' failed, expected exit code 1, got 127
|
||||
|
||||
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:
|
||||
|
||||
@test "invoking foo without arguments prints usage" {
|
||||
run -1 foo
|
||||
[ "${lines[0]}" = "usage: foo <filename>" ]
|
||||
}
|
||||
|
||||
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 BATS_PIPE HELPER
|
||||
--------------
|
||||
|
||||
Usage: bats_pipe [OPTIONS] [--] <command0...> [ \| <command1...> [ \| <command2...> [...] ] ]
|
||||
Options:
|
||||
-<N> return the exit code from the <N>th command in the chain
|
||||
of piped commands, instead of default behavior of "the last
|
||||
non-zero status".
|
||||
|
||||
The bats_pipe helper command is meant to handle piping between commands. Its
|
||||
main purpose is to aide the `run` helper command (which cannot handle pipes,
|
||||
due to bash parsing priority). `run command0 | command1` will parse `|` before
|
||||
`run`, which is commonly not intended by test authors.
|
||||
|
||||
Running `run bats_pipe command0 \| command1` will have the piped commands run
|
||||
within the context of the `run` command, and thus have the output and status
|
||||
variables properly contained within the normal `output` and `status` variables.
|
||||
|
||||
Note that this requires the usage of `\|`, not `|`. This is to avoid bash
|
||||
parsing out `|` first, instead sending `\|` to the bats_pipe command for it to
|
||||
parse and set up intended piping. Running bats_pipe with no instances of `\|`
|
||||
will always fail; this is intended to catch typos (accidentally using `|`) by
|
||||
the test author.
|
||||
|
||||
The bats_pipe command will also properly propagate exit status from the piped
|
||||
commands. The default behavior mimics `set -o pipefail`, returning the status
|
||||
of the last (rightmost) command that exits with a non-zero status. This ensures
|
||||
that usage of pipes do not mask the exit statuses of earlier commands.
|
||||
|
||||
@test "invoking foo piped to bar" {
|
||||
run bats_pipe foo \| bar
|
||||
# asserting foo or bar would return 17 (from foo if bar returns 0).
|
||||
[ "$status" -eq 17 ]
|
||||
[ "$output" = "bar output." ]
|
||||
}
|
||||
|
||||
Alternatively, if the test always cares about the status of a specific command,
|
||||
the -<N> option can be given (e.g. -0) to always return the status of the
|
||||
command of interest.
|
||||
|
||||
@test "invoking foo piped to bar always return foo status" {
|
||||
run bats_pipe -0 foo \| bar
|
||||
# status of bar is ignored, status is always from foo.
|
||||
[ "$status" -eq 2 ]
|
||||
[ "$output" = "bar output." ]
|
||||
}
|
||||
|
||||
Similarly, --returned-status N (or --returned-status=N) can be used for similar
|
||||
functionality. This option supports negative values, which always return the
|
||||
of the command starting from the end and in reverse order.
|
||||
|
||||
@test "invoking foo piped to bar always return foo status" {
|
||||
run bats_pipe --returned-status -2 foo \| bar
|
||||
# status of bar is ignored, status is always from foo.
|
||||
[ "$status" -eq 2 ]
|
||||
[ "$output" = "bar output." ]
|
||||
}
|
||||
|
||||
Piping of command output is especially helpful when the output needs to be
|
||||
modified in some way (e.g. the command outputs binary data into stdout, which
|
||||
cannot be stored as-is in an environment variable).
|
||||
|
||||
@test "invoking foo that returns binary data" {
|
||||
run bats_pipe foo \| hexdump -v -e "1/1 \"0x%02X \""
|
||||
[ "$status" -eq 17 ]
|
||||
[[ "$output" =~ 0xDE\ 0xAD ]]
|
||||
}
|
||||
|
||||
Any number of pipes can be used in conjunction to chain output between some set
|
||||
of running commands.
|
||||
|
||||
THE LOAD COMMAND
|
||||
----------------
|
||||
|
||||
You 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
|
||||
|
||||
load test_helper
|
||||
|
||||
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 BATS_LOAD_LIBRARY COMMAND
|
||||
-----------------------------
|
||||
|
||||
Some libraries are installed on the system, e.g. by `npm` or `brew`.
|
||||
These should not be `load`ed, as their path depends on the installation method.
|
||||
Instead, one should use `bats_load_library` together with setting
|
||||
`BATS_LIB_PATH`, a `PATH`-like colon-delimited variable.
|
||||
|
||||
`bats_load_library` has two modes of resolving requests:
|
||||
|
||||
1. by relative path from the `BATS_LIB_PATH` to a file in the library
|
||||
2. by library name, expecting libraries to have a `load.bash` entrypoint
|
||||
|
||||
For example if your `BATS_LIB_PATH` is set to
|
||||
`~/.bats/libs:/usr/lib/bats`, then `bats_load_library test_helper`
|
||||
would look for existing files with the following paths:
|
||||
|
||||
- `~/.bats/libs/test_helper`
|
||||
- `~/.bats/libs/test_helper/load.bash`
|
||||
- `/usr/lib/bats/test_helper`
|
||||
- `/usr/lib/bats/test_helper/load.bash`
|
||||
|
||||
The first existing file in this list will be sourced.
|
||||
|
||||
If you want to load only part of a library or the entry point is not named `load.bash`,
|
||||
you have to include it in the argument:
|
||||
`bats_load_library library_name/file_to_load` will try
|
||||
|
||||
- `~/.bats/libs/library_name/file_to_load`
|
||||
- `~/.bats/libs/library_name/file_to_load/load.bash`
|
||||
- `/usr/lib/bats/library_name/file_to_load`
|
||||
- `/usr/lib/bats/library_name/file_to_load/load.bash`
|
||||
|
||||
Apart from the changed lookup rules, `bats_load_library` behaves like `load`.
|
||||
|
||||
**Note**: As seen above `load.bash` is the entry point for libraries and
|
||||
meant to load more files from its directory or other libraries.
|
||||
|
||||
**Note**: Obviously, the actual `BATS_LIB_PATH` is highly dependent on the environment.
|
||||
To maintain a uniform location across systems, (distribution) package maintainers
|
||||
are encouraged to use `/usr/lib/bats/` as the install path for libraries where possible.
|
||||
However, if the package manager has another preferred location, like `npm` or `brew`,
|
||||
you should use this instead.
|
||||
|
||||
THE SKIP COMMAND
|
||||
----------------
|
||||
|
||||
Tests can be skipped by using the `skip` command at the point in a
|
||||
test you wish to skip.
|
||||
|
||||
@test "A test I don't want to execute for now" {
|
||||
skip
|
||||
run -0 foo
|
||||
}
|
||||
|
||||
Optionally, you may include a reason for skipping:
|
||||
|
||||
@test "A test I don't want to execute for now" {
|
||||
skip "This command will return zero soon, but not now"
|
||||
run -0 foo
|
||||
}
|
||||
|
||||
Or you can skip conditionally:
|
||||
|
||||
@test "A test which should run" {
|
||||
if [ foo != bar ]; then
|
||||
skip "foo isn't bar"
|
||||
fi
|
||||
|
||||
run -0 foo
|
||||
}
|
||||
|
||||
|
||||
THE BATS_REQUIRE_MINIMUM_VERSION COMMAND
|
||||
----------------------------------------
|
||||
|
||||
Code for newer versions of Bats can be incompatible with older versions.
|
||||
In the best case this will lead to an error message and a failed test suite.
|
||||
In the worst case, the tests will pass erroneously, potentially masking a failure.
|
||||
|
||||
Use `bats_require_minimum_version <Bats version number>` to avoid this.
|
||||
It communicates in a concise manner, that you intend the following code to be run
|
||||
under the given Bats version or higher.
|
||||
|
||||
Additionally, this function will communicate the current Bats version floor to
|
||||
subsequent code, allowing e.g. Bats' internal warning to give more informed warnings.
|
||||
|
||||
**Note**: By default, calling `bats_require_minimum_version` with versions before
|
||||
Bats 1.7.0 will fail regardless of the required version as the function is not
|
||||
available. However, you can use the
|
||||
bats-backports plugin (https://github.com/bats-core/bats-backports) to make
|
||||
your code usable with older versions, e.g. during migration while your CI system
|
||||
is not yet upgraded.
|
||||
|
||||
SETUP AND TEARDOWN FUNCTIONS
|
||||
----------------------------
|
||||
|
||||
You 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 CASES
|
||||
--------------------------
|
||||
|
||||
You 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 VARIABLES
|
||||
-----------------
|
||||
|
||||
There are several global variables you can use to introspect on Bats
|
||||
tests:
|
||||
|
||||
* `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test
|
||||
file.
|
||||
* `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is
|
||||
located.
|
||||
* `$BATS_TEST_NAMES` is an array of function names for each test case.
|
||||
* `$BATS_TEST_NAME` is the name of the function containing the current
|
||||
test case.
|
||||
* `BATS_TEST_NAME_PREFIX` will be prepended to the description of each test
|
||||
on stdout and in reports.
|
||||
* `$BATS_TEST_DESCRIPTION` is the description of the current test
|
||||
case.
|
||||
* `BATS_TEST_RETRIES` is the maximum number of additional attempts that will be
|
||||
made on a failed test before it is finally considered failed.
|
||||
The default of 0 means the test must pass on the first attempt.
|
||||
* `BATS_TEST_TIMEOUT` is the number of seconds after which a test (including setup)
|
||||
will be aborted and marked as failed. Updates to this value in `setup()` or `@test`
|
||||
cannot change the running timeout countdown, so the latest useful update location is `setup_file()`.
|
||||
* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
|
||||
in the test file.
|
||||
* `$BATS_SUITE_TEST_NUMBER` is the (1-based) index of the current test
|
||||
case in the test suite (over all files).
|
||||
* `$BATS_TMPDIR` is the base temporary directory used by bats to create its
|
||||
temporary files / directories.
|
||||
(default: `$TMPDIR`. If `$TMPDIR` is not set, `/tmp` is used.)
|
||||
* `$BATS_RUN_TMPDIR` is the location to the temporary directory used by
|
||||
bats to store all its internal temporary files during the tests.
|
||||
(default: `$BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX`)
|
||||
* `$BATS_FILE_EXTENSION` (default: `bats`) specifies the extension of
|
||||
test files that should be found when running a suite (via
|
||||
`bats [-r] suite_folder/`)
|
||||
* `$BATS_TEST_TAGS` the tags of the current test.
|
||||
* `$BATS_SUITE_TMPDIR` is a temporary directory common to all tests of a suite.
|
||||
Could be used to create files required by multiple tests.
|
||||
* `$BATS_FILE_TMPDIR` 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.
|
||||
* `$BATS_TEST_TMPDIR` is a temporary directory unique for each test.
|
||||
Could be used to create files required only for specific tests.
|
||||
* `$BATS_VERSION` is the version of Bats running the test.
|
||||
|
||||
|
||||
SEE ALSO
|
||||
--------
|
||||
|
||||
`bash`(1), `bats`(1)
|
||||
Reference in New Issue
Block a user