1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 03:32:27 +01:00

Document the run helper

This commit is contained in:
Sam Stephenson 2011-12-29 17:47:29 -06:00
parent 20b54ec582
commit f854037dd1

View File

@ -29,9 +29,10 @@ test passes. In this way, each line is an assertion of truth.
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
the results are written to standard output in human-readable TAP
format. If all the test cases pass, `bats` exits with a `0` status
code. If there is a failure, `bats` exits with a `1` status code.
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.
$ bats addition.bats
1..2
@ -46,3 +47,40 @@ fixtures, set up your environment, and clean up when you're done.
Bats is most useful when testing software written in Bash, but you can
use it to test any UNIX program.
### The `run` helper
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` exit status 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.
```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>" ]
}
```