- Notifications
You must be signed in to change notification settings - Fork515
Bash Automated Testing System
License
sstephenson/bats
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Bats is aTAP-compliant testing frameworkfor Bash. It provides a simple way to verify that the UNIX programsyou write behave as expected.
A Bats test file is a Bash script with special syntax for definingtest cases. Under the hood, each test case is just a function with adescription.
#!/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 ]}
Bats is most useful when testing software written in Bash, but you canuse it to test any UNIX program.
Test cases consist of standard shell commands. Bats makes use ofBash'serrexit
(set -e
) option when running test cases. If everycommand in the test case exits with a0
status code (success), thetest passes. In this way, each line is an assertion of truth.
To run your tests, invoke thebats
interpreter with a path to a testfile. The file's test cases are run sequentially and in isolation. Ifall the test cases pass,bats
exits with a0
status code. If thereare any failures,bats
exits with a1
status code.
When you run Bats from a terminal, you'll see output as each test isperformed, with a check-mark next to the test's name if it passes oran "X" if it fails.
$ bats addition.bats ✓ addition using bc ✓ addition using dc2 tests, 0 failures
If Bats is not connected to a terminal—in other words, if yourun it from a continuous integration system, or redirect its output toa file—the results are displayed in human-readable, machine-parsableTAP format.
You can force TAP output from a terminal by invoking Bats with the--tap
option.
$ bats --tap addition.bats1..2ok 1 addition using bcok 2 addition using dc
You can invoke thebats
interpreter with multiple test filearguments, or with a path to a directory containing multiple.bats
files. Bats will run each test file individually and aggregate theresults. If any test case fails,bats
exits with a1
status code.
Each Bats test file is evaluatedn+1 times, wheren is the number oftest 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 ownprocess.
For more details about how Bats evaluates test files, seeBats Evaluation Processon the wiki.
Many Bats tests need to run a command and then make assertions aboutits exit status and output. Bats includes arun
helper that invokesits arguments as a command, saves the exit status and output intospecial global variables, and then returns with a0
status code soyou can continue to make assertions in your test case.
For example, let's say you're testing that thefoo
command, whenpassed a nonexistent filename, exits with a1
status code and printsan error message.
@test"invoking foo with a nonexistent file prints an error" { run foo nonexistent_filename ["$status"-eq 1 ] ["$output"="foo: no such file 'nonexistent_filename'" ]}
The$status
variable contains the status code of the command, andthe$output
variable contains the combined contents of the command'sstandard output and standard error streams.
A third special variable, the$lines
array, is available for easilyaccessing individual lines of output. For example, if you want to testthat invokingfoo
without any arguments prints usage information onthe first line:
@test"invoking foo without arguments prints usage" { run foo ["$status"-eq 1 ] ["${lines[0]}"="usage: foo <filename>" ]}
You may want to share common code across multiple test files. Batsincludes a convenientload
command for sourcing a Bash source filerelative to the location of the current test file. For example, if youhave a Bats test intest/foo.bats
, the command
load test_helper
will source the scripttest/test_helper.bash
in your test file. Thiscan be useful for sharing functions to set up your environment or loadfixtures.
Tests can be skipped by using theskip
command at the point in atest you wish to skip.
@test"A test I don't want to execute for now" { skip run foo ["$status"-eq 0 ]}
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 foo ["$status"-eq 0 ]}
Or you can skip conditionally:
@test"A test which should run" {if [ foo!= bar ];then skip"foo isn't bar"fi run foo ["$status"-eq 0 ]}
You can define specialsetup
andteardown
functions, which runbefore and after each test case, respectively. Use these to loadfixtures, set up your environment, and clean up when you're done.
You can include code in your test file outside of@test
functions.For example, this may be useful if you want to check for dependenciesand fail immediately if they're not present. However, any output thatyou print in code outside of@test
,setup
orteardown
functionsmust be redirected tostderr
(>&2
). Otherwise, the output maycause Bats to fail by polluting the TAP stream onstdout
.
There are several global variables you can use to introspect on Batstests:
$BATS_TEST_FILENAME
is the fully expanded path to the Bats testfile.$BATS_TEST_DIRNAME
is the directory in which the Bats test file islocated.$BATS_TEST_NAMES
is an array of function names for each test case.$BATS_TEST_NAME
is the name of the function containing the currenttest case.$BATS_TEST_DESCRIPTION
is the description of the current testcase.$BATS_TEST_NUMBER
is the (1-based) index of the current test casein the test file.$BATS_TMPDIR
is the location to a directory that may be used tostore temporary files.
Check out a copy of the Bats repository. Then, either add the Batsbin
directory to your$PATH
, or run the providedinstall.sh
command with the location to the prefix in which you want to installBats. For example, to install Bats into/usr/local
,
$ git clone https://github.com/sstephenson/bats.git$ cd bats$ ./install.sh /usr/local
Note that you may need to runinstall.sh
withsudo
if you do nothave permission to write to the installation prefix.
The Bats source code repository ishosted onGitHub. There you can file bugson the issue tracker or submit tested pull requests for review.
For real-world examples from open-source projects using Bats, seeProjects Using Batson the wiki.
To learn how to set up your editor for Bats syntax highlighting, seeSyntax Highlightingon the wiki.
0.4.0 (August 13, 2014)
- Improved the display of failing test cases. Bats now shows thesource code of failing test lines, along with full stack tracesincluding function names, filenames, and line numbers.
- Improved the display of the pretty-printed test summary line toinclude the number of skipped tests, if any.
- Improved the speed of the preprocessor, dramatically shortening testand suite startup times.
- Added support for absolute pathnames to the
load
helper. - Added support for single-line
@test
definitions. - Added bats(1) and bats(7) manual pages.
- Modified the
bats
command to default to TAP output when the$CI
variable is set, to better support environments such as Travis CI.
0.3.1 (October 28, 2013)
- Fixed an incompatibility with the pretty formatter in certainenvironments such as tmux.
- Fixed a bug where the pretty formatter would crash if the first lineof a test file's output was invalid TAP.
0.3.0 (October 21, 2013)
- Improved formatting for tests run from a terminal. Failing testsare now colored in red, and the total number of failing tests isdisplayed at the end of the test run. When Bats is not connected toa terminal (e.g. in CI runs), or when invoked with the
--tap
flag,output is displayed in standard TAP format. - Added the ability to skip tests using the
skip
command. - Added a message to failing test case output indicating the file andline number of the statement that caused the test to fail.
- Added "ad-hoc" test suite support. You can now invoke
bats
withmultiple filename or directory arguments to run all the specifiedtests in aggregate. - Added support for test files with Windows line endings.
- Fixed regular expression warnings from certain versions of Bash.
- Fixed a bug running tests containing lines that begin with
-e
.
0.2.0 (November 16, 2012)
- Added test suite support. The
bats
command accepts a directoryname containing multiple test files to be run in aggregate. - Added the ability to count the number of test cases in a file orsuite by passing the
-c
flag tobats
. - Preprocessed sources are cached between test case runs in the samefile for better performance.
0.1.0 (December 30, 2011)
- Initial public release.
© 2014 Sam Stephenson. Bats is released under an MIT-style license;seeLICENSE
for details.
About
Bash Automated Testing System
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.