1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 19:52:37 +01:00
bats/README.md

181 lines
5.7 KiB
Markdown
Raw Normal View History

2011-12-29 23:28:01 +01:00
# Bats: the Bash Automated Testing System
Bats is a [TAP](http://testanything.org/)-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.
```bash
#!/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 ]
}
```
2012-11-16 22:32:30 +01:00
Bats is most useful when testing software written in Bash, but you can
use it to test any UNIX program.
2011-12-29 23:28:01 +01:00
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.
2012-11-16 22:32:30 +01:00
## Running tests
2011-12-29 23:28:01 +01:00
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, and
2011-12-30 00:47:29 +01:00
the results are written to standard output in human-readable [TAP
format](http://testanything.org/wiki/index.php/TAP_specification#THE_TAP_FORMAT).
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.
2011-12-29 23:28:01 +01:00
$ bats addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc
$ echo $?
0
You can also 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.
2012-11-16 22:32:30 +01:00
### Test suites
You can also invoke the `bats` interpreter 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.
2011-12-30 00:47:29 +01:00
2012-11-17 00:57:30 +01:00
## Helpers and introspection
2011-12-30 21:04:14 +01:00
2011-12-30 00:58:40 +01:00
### The _run_ helper
2011-12-30 00:47:29 +01:00
2011-12-30 21:04:14 +01:00
If you're using Bats, you're probably most interested in testing a
command's 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 then returns with a `0` status code
so you can continue to make assertions in your test case.
2011-12-30 00:47:29 +01:00
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.
```bash
@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, 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:
```bash
@test "invoking foo without arguments prints usage" {
run foo
[ "$status" -eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ]
}
```
2011-12-30 00:58:40 +01:00
### 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
```bash
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.
2011-12-30 01:02:36 +01:00
### 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_DESCRIPTION` is the description of the current test
case.
* `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
in the test file.
* `$BATS_TMPDIR` is the location to a directory that may be used to
store temporary files.
2011-12-30 21:04:14 +01:00
## Installing Bats
2011-12-30 01:48:33 +01:00
Check out a copy of the Bats repository. Then, either add the Bats
`bin` directory to your `$PATH`, or run the provided `install.sh`
command with the location to the prefix in which you want to install
Bats. 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 run `install.sh` with `sudo` if you do not
have permission to write to the installation prefix.
2013-05-04 00:21:15 +02:00
## Syntax Highlighting
* [Bats.tmbundle](https://github.com/drnic/Bats.tmbundle) from Dr Nic
Williams adds Bats syntax highlighting support for TextMate.
2011-12-30 21:13:41 +01:00
## Development
2011-12-30 01:56:17 +01:00
The Bats source code repository is [hosted on
GitHub](https://github.com/sstephenson/bats). There you can file bugs
on the issue tracker or submit tested pull requests for review.
See the [Bats
test suite](https://github.com/sstephenson/bats/tree/master/test) for
examples.
2011-12-30 21:13:41 +01:00
### Version history
2012-11-17 01:06:58 +01:00
*0.2.0* (November 16, 2012)
* Added test suite support. The `bats` command accepts a directory
name containing multiple test files to be run in aggregate.
* Added the ability to count the number of test cases in a file or
suite by passing the `-c` flag to `bats`.
* Preprocessed sources are cached between test case runs in the same
file for better performance.
2011-12-30 21:13:41 +01:00
*0.1.0* (December 30, 2011)
* Initial public release.
2011-12-30 01:50:37 +01:00
---
© 2011 Sam Stephenson. Bats is released under an MIT-style license;
see `LICENSE` for details.