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 --output {merged,separate,std‐
37       out,stderr} control which output is recorded --keep-empty-lines  retain
38       emtpy lines in ${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 run --output <mode> to control what goes into
98       $output and $lines. The available values for <mode> are:
99
100merged: the default when --output  is  not  specified,  interleaves
101           stdout and stderr
102
103separate: splits stderr off to $stderr and ${stderr_lines[@]}, std‐
104           out is still available as $output and ${lines[@]}
105
106stderr: discards stdout and fills ´$stderrand${stderr_lines[@]}`
107
108stdout: discards stdout and fills $output and ${lines[@]}
109
110
111
112       All additional parameters to run should come before the command. If you
113       want  to run a command that starts with -, prefix it with -- to prevent
114       run from parsing it as an option.
115

THE LOAD COMMAND

117       You may want to share common code across multiple test files. Bats  in‐
118       cludes  a convenient load command for sourcing a Bash source file rela‐
119       tive to the location of the current test file. For example, if you have
120       a Bats test in test/foo.bats, the command
121
122
123
124           load test_helper
125
126
127
128       will  source  the  script test/test_helper.bash in your test file. This
129       can be useful for sharing functions to set up your environment or  load
130       fixtures.
131

THE SKIP COMMAND

133       Tests  can  be skipped by using the skip command at the point in a test
134       you wish to skip.
135
136
137
138           @test "A test I don´t want to execute for now" {
139             skip
140             run -0 foo
141           }
142
143
144
145       Optionally, you may include a reason for skipping:
146
147
148
149           @test "A test I don´t want to execute for now" {
150             skip "This command will return zero soon, but not now"
151             run -0 foo
152           }
153
154
155
156       Or you can skip conditionally:
157
158
159
160           @test "A test which should run" {
161             if [ foo != bar ]; then
162               skip "foo isn´t bar"
163             fi
164
165             run -0 foo
166           }
167
168
169

SETUP AND TEARDOWN FUNCTIONS

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

CODE OUTSIDE OF TEST CASES

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

SPECIAL VARIABLES

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

SEE ALSO

231       bash(1), bats(1)
232
233
234
235bats-core                         August 2021                          BATS(7)
Impressum