mirror of
https://github.com/sstephenson/bats.git
synced 2026-02-25 17:28:11 +01:00
Document the run helper
This commit is contained in:
44
README.md
44
README.md
@@ -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
|
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
|
file. The file's test cases are run sequentially and in isolation, and
|
||||||
the results are written to standard output in human-readable TAP
|
the results are written to standard output in human-readable [TAP
|
||||||
format. If all the test cases pass, `bats` exits with a `0` status
|
format](http://testanything.org/wiki/index.php/TAP_specification#THE_TAP_FORMAT).
|
||||||
code. If there is a failure, `bats` exits with a `1` status code.
|
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
|
$ bats addition.bats
|
||||||
1..2
|
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
|
Bats is most useful when testing software written in Bash, but you can
|
||||||
use it to test any UNIX program.
|
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>" ]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user