1BATS(7)                  Bash Automated Testing System                 BATS(7)
2
3
4

NAME

6       bats - Bats test file format
7

DESCRIPTION

9       A Bats test file is a Bash script with special syntax for defining test
10       cases. Under the hood, each test case is just a  function  with  a  de‐
11       scription.
12
13
14
15           #!/usr/bin/env bats
16
17           @test "addition using bc" {
18             result="$(echo 2+2 | bc)"
19             [ "$result" -eq 4 ]
20           }
21
22           @test "addition using dc" {
23             result="$(echo 2 2+p | dc)"
24             [ "$result" -eq 4 ]
25           }
26
27
28
29       Each  Bats  test  file is evaluated n+1 times, where n is the number of
30       test cases in the file. The first run counts the number of test  cases,
31       then  iterates  over  the  test  cases and executes each one in its own
32       process.
33

THE RUN HELPER

35       Usage: run [OPTIONS] [--] <command...> Options: ! check  for  non  zero
36       exit  code  -N check that exit code is N --separate-stderr split stderr
37       and    stdout    --keep-empty-lines    retain    emtpy     lines     in
38       ${lines[@]}/${stderr_lines[@]}
39
40       Many  Bats  tests  need to run a command and then make assertions about
41       its exit status and output. Bats includes a run helper that invokes its
42       arguments  as  a command, saves the exit status and output into special
43       global variables, and (optionally) checks exit status against  a  given
44       expected  value. If successful, run returns with a 0 status code so you
45       can continue to make assertions in your test case.
46
47       For example, let´s say you´re testing that the foo command, when passed
48       a  nonexistent filename, exits with a 1 status code and prints an error
49       message.
50
51
52
53           @test "invoking foo with a nonexistent file prints an error" {
54             run -1 foo nonexistent_filename
55             [ "$output" = "foo: no such file ´nonexistent_filename´" ]
56           }
57
58
59
60       The -1 as first argument tells run to expect 1 as an exit  status,  and
61       to fail if the command exits with any other value. On failure, both ac‐
62       tual and expected values will be displayed, along with the invoked com‐
63       mand and its output:
64
65
66
67           (in test file test.bats, line 2)
68            `run -1 foo nonexistent_filename´ failed, expected exit code 1, got 127
69
70
71
72       This  error  indicates a possible problem with the installation or con‐
73       figuration of foo; note that a simple [ $status != 0 ] test  would  not
74       have caught this kind of failure.
75
76       The  $status  variable contains the status code of the command, and the
77       $output variable contains the combined contents of the command´s  stan‐
78       dard output and standard error streams.
79
80       A third special variable, the $lines array, is available for easily ac‐
81       cessing individual lines of output. For example, if you  want  to  test
82       that invoking foo without any arguments prints usage information on the
83       first line:
84
85
86
87           @test "invoking foo without arguments prints usage" {
88             run -1 foo
89             [ "${lines[0]}" = "usage: foo <filename>" ]
90           }
91
92
93
94       By  default  run  leaves  out  empty  lines  in  ${lines[@]}.  Use  run
95       --keep-empty-lines to retain them.
96
97       Additionally,  you can use --separate-stderr to split stdout and stderr
98       into $output/$stderr and ${lines[@]}/${stderr_lines[@]}.
99
100       All additional parameters to run should come before the command. If you
101       want  to run a command that starts with -, prefix it with -- to prevent
102       run from parsing it as an option.
103

THE LOAD COMMAND

105       You may want to share common code across multiple test files. Bats  in‐
106       cludes  a convenient load command for sourcing a Bash source file rela‐
107       tive to the location of the current test file. For example, if you have
108       a Bats test in test/foo.bats, the command
109
110
111
112           load test_helper
113
114
115
116       will  source  the  script test/test_helper.bash in your test file. This
117       can be useful for sharing functions to set up your environment or  load
118       fixtures.
119

THE SKIP COMMAND

121       Tests  can  be skipped by using the skip command at the point in a test
122       you wish to skip.
123
124
125
126           @test "A test I don´t want to execute for now" {
127             skip
128             run -0 foo
129           }
130
131
132
133       Optionally, you may include a reason for skipping:
134
135
136
137           @test "A test I don´t want to execute for now" {
138             skip "This command will return zero soon, but not now"
139             run -0 foo
140           }
141
142
143
144       Or you can skip conditionally:
145
146
147
148           @test "A test which should run" {
149             if [ foo != bar ]; then
150               skip "foo isn´t bar"
151             fi
152
153             run -0 foo
154           }
155
156
157

SETUP AND TEARDOWN FUNCTIONS

159       You can define special setup and teardown functions  which  run  before
160       and after each test case, respectively. Use these to load fixtures, set
161       up your environment, and clean up when you´re done.
162

CODE OUTSIDE OF TEST CASES

164       You can include code in your test file outside of @test functions.  For
165       example,  this  may be useful if you want to check for dependencies and
166       fail immediately if they´re not present. However, any output  that  you
167       print  in  code  outside  of @test, setup or teardown functions must be
168       redirected to stderr (>&2). Otherwise, the output  may  cause  Bats  to
169       fail by polluting the TAP stream on stdout.
170

SPECIAL VARIABLES

172       There  are  several  global variables you can use to introspect on Bats
173       tests:
174
175$BATS_TEST_FILENAME is the fully expanded path  to  the  Bats  test
176           file.
177
178$BATS_TEST_DIRNAME  is the directory in which the Bats test file is
179           located.
180
181$BATS_TEST_NAMES is an array of function names for each test case.
182
183$BATS_TEST_NAME is the name of the function containing the  current
184           test case.
185
186$BATS_TEST_DESCRIPTION is the description of the current test case.
187
188$BATS_TEST_NUMBER  is  the (1-based) index of the current test case
189           in the test file.
190
191$BATS_SUITE_TEST_NUMBER is the (1-based) index of the current  test
192           case in the test suite (over all files).
193
194$BATS_TMPDIR is the base temporary directory used by bats to create
195           its temporary files / directories. (default: $TMPDIR. If $TMPDIR is
196           not set, /tmp is used.)
197
198$BATS_RUN_TMPDIR is the location to the temporary directory used by
199           bats to store all its internal temporary files  during  the  tests.
200           (default: $BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX)
201
202$BATS_FILE_EXTENSION  (default:  bats)  specifies  the extension of
203           test files that should be found when running a suite (via bats [-r]
204           suite_folder/)
205
206$BATS_SUITE_TMPDIR  is a temporary directory common to all tests of
207           a suite. Could be used to create files required by multiple tests.
208
209$BATS_FILE_TMPDIR is a temporary directory common to all tests of a
210           test file. Could be used to create files required by multiple tests
211           in the same test file.
212
213$BATS_TEST_TMPDIR is a temporary directory unique  for  each  test.
214           Could be used to create files required only for specific tests.
215
216$BATS_VERSION is the version of Bats running the test.
217
218
219

SEE ALSO

221       bash(1), bats(1)
222
223
224
225bats-core                        November 2021                         BATS(7)
Impressum