1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-26 06:29:47 +01:00

Update the man pages

This commit is contained in:
Sam Stephenson 2013-11-10 16:03:38 -06:00
parent b1eee9f455
commit 225440bb65
7 changed files with 88 additions and 612 deletions

View File

@ -9,13 +9,22 @@
.SH "SYNOPSIS" .SH "SYNOPSIS"
bats [\-c] [\-p | \-t] \fItest\fR [\fItest\fR \.\.\.] bats [\-c] [\-p | \-t] \fItest\fR [\fItest\fR \.\.\.]
. .
.P
\fItest\fR is the path to a Bats test file, or the path to a directory containing Bats test files\.
.
.SH "DESCRIPTION" .SH "DESCRIPTION"
Bats is a TAP\-compliant testing framework for Bash\. It provides a simple way to verify that the UNIX programs you write behave as expected\. Bats is a TAP\-compliant testing framework for Bash\. It provides a simple way to verify that the UNIX programs you write behave as expected\.
. .
.P .P
Bats is most useful when testing software written in Bash, but you can use it to test any UNIX program\. 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\.
. .
.SH "FILES" .P
Test cases consist of standard shell commands\. Bats makes use of Bash\'s \fBerrexit\fR (\fBset \-e\fR) option when running test cases\. If every command in the test case exits with a \fB0\fR status code (success), the test passes\. In this way, each line is an assertion of truth\.
.
.P
See \fBbats\fR(7) for more information on writing Bats tests\.
.
.SH "RUNNING TESTS"
To run your tests, invoke the \fBbats\fR interpreter with a path to a test file\. The file\'s test cases are run sequentially and in isolation\. If all the test cases pass, \fBbats\fR exits with a \fB0\fR status code\. If there are any failures, \fBbats\fR exits with a \fB1\fR status code\. To run your tests, invoke the \fBbats\fR interpreter with a path to a test file\. The file\'s test cases are run sequentially and in isolation\. If all the test cases pass, \fBbats\fR exits with a \fB0\fR status code\. If there are any failures, \fBbats\fR exits with a \fB1\fR status code\.
. .
.P .P
@ -29,7 +38,7 @@ Count the number of test cases without running any tests
. .
.TP .TP
\fB\-h\fR, \fB\-\-help\fR \fB\-h\fR, \fB\-\-help\fR
Display this help message Display help message
. .
.TP .TP
\fB\-p\fR, \fB\-\-pretty\fR \fB\-p\fR, \fB\-\-pretty\fR
@ -43,7 +52,7 @@ Show results in TAP format
\fB\-v\fR, \fB\-\-version\fR \fB\-v\fR, \fB\-\-version\fR
Display the version number Display the version number
. .
.SH "EXAMPLES" .SH "OUTPUT"
When you run Bats from a terminal, you\'ll see output as each test is performed, with a check\-mark next to the test\'s name if it passes or an "X" if it fails\. When you run Bats from a terminal, you\'ll see output as each test is performed, with a check\-mark next to the test\'s name if it passes or an "X" if it fails\.
. .
.IP "" 4 .IP "" 4
@ -61,7 +70,7 @@ $ bats addition\.bats
.IP "" 0 .IP "" 0
. .
.P .P
If Bats is not connected to a terminal—in other words, if you run it from a continuous integration system or redirect its output to a file—the results are displayed in human\-readable, machine\-parsable TAP format \fIhttp://testanything\.org/wiki/index\.php/TAP_specification#THE_TAP_FORMAT\fR\. You can force TAP output from a terminal by invoking Bats with the \fB\-\-tap\fR option\. If Bats is not connected to a terminal\-\-in other words, if you run it from a continuous integration system or redirect its output to a file\-\-the results are displayed in human\-readable, machine\-parsable TAP format\. You can force TAP output from a terminal by invoking Bats with the \fB\-\-tap\fR option\.
. .
.IP "" 4 .IP "" 4
. .
@ -76,11 +85,17 @@ ok 2 addition using dc
. .
.IP "" 0 .IP "" 0
. .
.SH "COPYRIGHT" .SH "EXIT STATUS"
(c) 2013 Sam Stephenson\. The \fBbats\fR interpreter exits with a value of \fB0\fR if all test cases pass, or \fB1\fR if one or more test cases fail\.
.
.P
Bats is released under an MIT\-style license
. .
.SH "SEE ALSO" .SH "SEE ALSO"
bats(7) Bats wiki: \fIhttps://github\.com/sstephenson/bats/wiki/\fR
.
.P
\fBbash\fR(1), \fBbats\fR(7)
.
.SH "COPYRIGHT"
(c) 2013 Sam Stephenson
.
.P
Bats is released under the terms of an MIT\-style license\.

View File

@ -1,156 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'>
<title>bats(1) - Bash Automated Testing System</title>
<style type='text/css' media='all'>
/* style: man */
body#manpage {margin:0}
.mp {max-width:100ex;padding:0 9ex 1ex 4ex}
.mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
.mp h2 {margin:10px 0 0 0}
.mp > p,.mp > pre,.mp > ul,.mp > ol,.mp > dl {margin-left:8ex}
.mp h3 {margin:0 0 0 4ex}
.mp dt {margin:0;clear:left}
.mp dt.flush {float:left;width:8ex}
.mp dd {margin:0 0 0 9ex}
.mp h1,.mp h2,.mp h3,.mp h4 {clear:left}
.mp pre {margin-bottom:20px}
.mp pre+h2,.mp pre+h3 {margin-top:22px}
.mp h2+pre,.mp h3+pre {margin-top:5px}
.mp img {display:block;margin:auto}
.mp h1.man-title {display:none}
.mp,.mp code,.mp pre,.mp tt,.mp kbd,.mp samp,.mp h3,.mp h4 {font-family:monospace;font-size:14px;line-height:1.42857142857143}
.mp h2 {font-size:16px;line-height:1.25}
.mp h1 {font-size:20px;line-height:2}
.mp {text-align:justify;background:#fff}
.mp,.mp code,.mp pre,.mp pre code,.mp tt,.mp kbd,.mp samp {color:#131211}
.mp h1,.mp h2,.mp h3,.mp h4 {color:#030201}
.mp u {text-decoration:underline}
.mp code,.mp strong,.mp b {font-weight:bold;color:#131211}
.mp em,.mp var {font-style:italic;color:#232221;text-decoration:none}
.mp a,.mp a:link,.mp a:hover,.mp a code,.mp a pre,.mp a tt,.mp a kbd,.mp a samp {color:#0000ff}
.mp b.man-ref {font-weight:normal;color:#434241}
.mp pre {padding:0 4ex}
.mp pre code {font-weight:normal;color:#434241}
.mp h2+pre,h3+pre {padding-left:0}
ol.man-decor,ol.man-decor li {margin:3px 0 10px 0;padding:0;float:left;width:33%;list-style-type:none;text-transform:uppercase;color:#999;letter-spacing:1px}
ol.man-decor {width:100%}
ol.man-decor li.tl {text-align:left}
ol.man-decor li.tc {text-align:center;letter-spacing:4px}
ol.man-decor li.tr {text-align:right;float:right}
</style>
</head>
<!--
The following styles are deprecated and will be removed at some point:
div#man, div#man ol.man, div#man ol.head, div#man ol.man.
The .man-page, .man-decor, .man-head, .man-foot, .man-title, and
.man-navigation should be used instead.
-->
<body id='manpage'>
<div class='mp' id='man'>
<div class='man-navigation' style='display:none'>
<a href="#NAME">NAME</a>
<a href="#SYNOPSIS">SYNOPSIS</a>
<a href="#DESCRIPTION">DESCRIPTION</a>
<a href="#FILES">FILES</a>
<a href="#OPTIONS">OPTIONS</a>
<a href="#EXAMPLES">EXAMPLES</a>
<a href="#COPYRIGHT">COPYRIGHT</a>
<a href="#SEE-ALSO">SEE ALSO</a>
</div>
<ol class='man-decor man-head man head'>
<li class='tl'>bats(1)</li>
<li class='tc'></li>
<li class='tr'>bats(1)</li>
</ol>
<h2 id="NAME">NAME</h2>
<p class="man-name">
<code>bats</code> - <span class="man-whatis">Bash Automated Testing System</span>
</p>
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p>bats [-c] [-p | -t] <var>test</var> [<var>test</var> ...]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p>Bats is a TAP-compliant testing framework for Bash.
It provides a simple way to verify that the UNIX programs you write behave as expected.</p>
<p>Bats is most useful when testing software written in Bash, but you can use it to test any UNIX program.</p>
<h2 id="FILES">FILES</h2>
<p>To run your tests, invoke the <code>bats</code> interpreter with a path to a test
file. The file's test cases are run sequentially and in isolation. If
all the test cases pass, <code>bats</code> exits with a <code>0</code> status code. If there
are any failures, <code>bats</code> exits with a <code>1</code> status code.</p>
<p>You can invoke the <code>bats</code> interpreter with multiple test file
arguments, or with a path to a directory containing multiple <code>.bats</code>
files. Bats will run each test file individually and aggregate the
results. If any test case fails, <code>bats</code> exits with a <code>1</code> status code.</p>
<h2 id="OPTIONS">OPTIONS</h2>
<dl>
<dt><code>-c</code>, <code>--count</code></dt><dd>Count the number of test cases without running any tests</dd>
<dt><code>-h</code>, <code>--help</code></dt><dd>Display this help message</dd>
<dt><code>-p</code>, <code>--pretty</code></dt><dd>Show results in pretty format (default for terminals)</dd>
<dt><code>-t</code>, <code>--tap</code></dt><dd>Show results in TAP format</dd>
<dt><code>-v</code>, <code>--version</code></dt><dd>Display the version number</dd>
</dl>
<h2 id="EXAMPLES">EXAMPLES</h2>
<p>When you run Bats from a terminal, you'll see output as each test is
performed, with a check-mark next to the test's name if it passes or
an "X" if it fails.</p>
<pre><code>$ bats addition.bats
✓ addition using bc
✓ addition using dc
2 tests, 0 failures
</code></pre>
<p>If Bats is not connected to a terminal—in other words, if you
run it from a continuous integration system or redirect its output to
a file—the results are displayed in human-readable, machine-parsable
<a href="http://testanything.org/wiki/index.php/TAP_specification#THE_TAP_FORMAT">TAP format</a>.
You can force TAP output from a terminal by invoking Bats with the
<code>--tap</code> option.</p>
<pre><code>$ bats --tap addition.bats
1..2
ok 1 addition using bc
ok 2 addition using dc
</code></pre>
<h2 id="COPYRIGHT">COPYRIGHT</h2>
<p>(c) 2013 Sam Stephenson.</p>
<p>Bats is released under an MIT-style license</p>
<h2 id="SEE-ALSO">SEE ALSO</h2>
<p><a class="man-ref" href="bats.7.html">bats<span class="s">(7)</span></a></p>
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>November 2013</li>
<li class='tr'>bats(1)</li>
</ol>
</div>
</body>
</html>

View File

@ -1,34 +1,46 @@
bats(1) -- Bash Automated Testing System bats(1) -- Bash Automated Testing System
======================================== ========================================
SYNOPSIS SYNOPSIS
-------- --------
bats [-c] [-p | -t] <test> [<test> ...] bats [-c] [-p | -t] <test> [<test> ...]
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files.
DESCRIPTION DESCRIPTION
----------- -----------
Bats is a TAP-compliant testing framework for Bash. It provides a simple
way to verify that the UNIX programs you write behave as expected.
Bats is a TAP-compliant testing framework for Bash. A Bats test file is a Bash script with special syntax for defining
It provides a simple way to verify that the UNIX programs you write behave as expected. test cases. Under the hood, each test case is just a function with a
description.
Bats is most useful when testing software written in Bash, but you can use it to test any UNIX program. 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.
See `bats`(7) for more information on writing Bats tests.
FILES RUNNING TESTS
----- -------------
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. If file. The file's test cases are run sequentially and in isolation. If
all the test cases pass, `bats` exits with a `0` status code. If there all the test cases pass, `bats` exits with a `0` status code. If there
are any failures, `bats` exits with a `1` status code. are any failures, `bats` exits with a `1` status code.
You can invoke the `bats` interpreter with multiple test file You can invoke the `bats` interpreter with multiple test file arguments,
arguments, or with a path to a directory containing multiple `.bats` or with a path to a directory containing multiple `.bats` files. Bats
files. Bats will run each test file individually and aggregate the will run each test file individually and aggregate the results. If any
results. If any test case fails, `bats` exits with a `1` status code. test case fails, `bats` exits with a `1` status code.
OPTIONS OPTIONS
@ -37,7 +49,7 @@ OPTIONS
* `-c`, `--count`: * `-c`, `--count`:
Count the number of test cases without running any tests Count the number of test cases without running any tests
* `-h`, `--help`: * `-h`, `--help`:
Display this help message Display help message
* `-p`, `--pretty`: * `-p`, `--pretty`:
Show results in pretty format (default for terminals) Show results in pretty format (default for terminals)
* `-t`, `--tap`: * `-t`, `--tap`:
@ -45,10 +57,9 @@ OPTIONS
* `-v`, `--version`: * `-v`, `--version`:
Display the version number Display the version number
EXAMPLES
--------
OUTPUT
------
When you run Bats from a terminal, you'll see output as each test is When you run Bats from a terminal, you'll see output as each test is
performed, with a check-mark next to the test's name if it passes or performed, with a check-mark next to the test's name if it passes or
@ -60,12 +71,11 @@ an "X" if it fails.
2 tests, 0 failures 2 tests, 0 failures
If Bats is not connected to a terminal—in other words, if you If Bats is not connected to a terminal--in other words, if you run it
run it from a continuous integration system or redirect its output to from a continuous integration system or redirect its output to a
a file—the results are displayed in human-readable, machine-parsable file--the results are displayed in human-readable, machine-parsable
[TAP format](http://testanything.org/wiki/index.php/TAP_specification#THE_TAP_FORMAT). TAP format. You can force TAP output from a terminal by invoking Bats
You can force TAP output from a terminal by invoking Bats with the with the `--tap` option.
`--tap` option.
$ bats --tap addition.bats $ bats --tap addition.bats
1..2 1..2
@ -73,15 +83,27 @@ You can force TAP output from a terminal by invoking Bats with the
ok 2 addition using dc ok 2 addition using dc
COPYRIGHT EXIT STATUS
--------- -----------
(c) 2013 Sam Stephenson. The `bats` interpreter exits with a value of `0` if all test cases pass,
or `1` if one or more test cases fail.
Bats is released under an MIT-style license
SEE ALSO SEE ALSO
-------- --------
bats(7) Bats wiki: _https://github.com/sstephenson/bats/wiki/_
`bash`(1), `bats`(7)
COPYRIGHT
---------
(c) 2013 Sam Stephenson
Bats is released under the terms of an MIT-style license.

View File

@ -6,61 +6,6 @@
.SH "NAME" .SH "NAME"
\fBbats\fR \- Bats test file format \fBbats\fR \- Bats test file format
. .
.SH "SYNOPSIS"
.
.nf
load test_helper
setup() {
# set up your environment
# run before and after each test case
}
teardown() {
# clean up your environment
# run before and after each test case
}
code_outside_of_test_cases () {
# For example, check for dependencies
# and fail immediatelyif they\'re not present\.
# Output must be redirected to `stderr` (`>&2`)
}
@test "test description" {
run foo arguments
[ "$status" \-eq 1 ]
[ "$output" = "expected output" ]
}
@test "test description" {
run foo arguments
[ "$status" \-eq 1 ]
[ "${lines[0]}" = "first line of expected output" ]
}
@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 ]
}
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn\'t bar"
fi
run foo
[ "$status" \-eq 0 ]
}
.
.fi
.
.SH "DESCRIPTION" .SH "DESCRIPTION"
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\. 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\.
. .
@ -87,10 +32,7 @@ A Bats test file is a Bash script with special syntax for defining test cases\.
.P .P
Each Bats test file is evaulated n+1 times, where \fIn\fR is the number of test 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 own process\. Each Bats test file is evaulated n+1 times, where \fIn\fR is the number of test 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 own process\.
. .
.P .SH "THE RUN HELPER"
For details about exactly how Bats evaluates test files, see Bats Evaluation Process: https://github\.com/sstephenson/bats/wiki/Bats\-Evaluation\-Process
.
.SH "THE <code>_RUN_</code> HELPER"
Many Bats tests need to run a command and then make assertions about its exit status and output\. Bats includes a \fBrun\fR helper that invokes its arguments as a command, saves the exit status and output into special global variables, and then returns with a \fB0\fR status code so you can continue to make assertions in your test case\. Many Bats tests need to run a command and then make assertions about its exit status and output\. Bats includes a \fBrun\fR helper that invokes its arguments as a command, saves the exit status and output into special global variables, and then returns with a \fB0\fR status code so you can continue to make assertions in your test case\.
. .
.P .P
@ -120,7 +62,8 @@ A third special variable, the \fB$lines\fR array, is available for easily access
. .
.nf .nf
@test "invoking foo without arguments prints usage" { run foo @test "invoking foo without arguments prints usage" {
run foo
[ "$status" \-eq 1 ] [ "$status" \-eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ] [ "${lines[0]}" = "usage: foo <filename>" ]
} }
@ -129,7 +72,7 @@ A third special variable, the \fB$lines\fR array, is available for easily access
. .
.IP "" 0 .IP "" 0
. .
.SH "THE <code>_LOAD_</code> COMMAND" .SH "THE LOAD COMMAND"
You may want to share common code across multiple test files\. Bats includes a convenient \fBload\fR 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 \fBtest/foo\.bats\fR, the command You may want to share common code across multiple test files\. Bats includes a convenient \fBload\fR 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 \fBtest/foo\.bats\fR, the command
. .
.IP "" 4 .IP "" 4
@ -145,7 +88,7 @@ load test_helper
.P .P
will source the script \fBtest/test_helper\.bash\fR in your test file\. This can be useful for sharing functions to set up your environment or load fixtures\. will source the script \fBtest/test_helper\.bash\fR in your test file\. This can be useful for sharing functions to set up your environment or load fixtures\.
. .
.SH "THE <code>_SKIP_</code> COMMAND" .SH "THE SKIP COMMAND"
Tests can be skipped by using the \fBskip\fR command at the point in a test you wish to skip\. Tests can be skipped by using the \fBskip\fR command at the point in a test you wish to skip\.
. .
.IP "" 4 .IP "" 4
@ -232,4 +175,4 @@ There are several global variables you can use to introspect on Bats tests:
.IP "" 0 .IP "" 0
. .
.SH "SEE ALSO" .SH "SEE ALSO"
bats(1) \fBbash\fR(1), \fBbats\fR(1)

View File

@ -1,290 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' value='text/html;charset=utf8'>
<meta name='generator' value='Ronn/v0.7.3 (http://github.com/rtomayko/ronn/tree/0.7.3)'>
<title>bats(7) - Bats test file format</title>
<style type='text/css' media='all'>
/* style: man */
body#manpage {margin:0}
.mp {max-width:100ex;padding:0 9ex 1ex 4ex}
.mp p,.mp pre,.mp ul,.mp ol,.mp dl {margin:0 0 20px 0}
.mp h2 {margin:10px 0 0 0}
.mp > p,.mp > pre,.mp > ul,.mp > ol,.mp > dl {margin-left:8ex}
.mp h3 {margin:0 0 0 4ex}
.mp dt {margin:0;clear:left}
.mp dt.flush {float:left;width:8ex}
.mp dd {margin:0 0 0 9ex}
.mp h1,.mp h2,.mp h3,.mp h4 {clear:left}
.mp pre {margin-bottom:20px}
.mp pre+h2,.mp pre+h3 {margin-top:22px}
.mp h2+pre,.mp h3+pre {margin-top:5px}
.mp img {display:block;margin:auto}
.mp h1.man-title {display:none}
.mp,.mp code,.mp pre,.mp tt,.mp kbd,.mp samp,.mp h3,.mp h4 {font-family:monospace;font-size:14px;line-height:1.42857142857143}
.mp h2 {font-size:16px;line-height:1.25}
.mp h1 {font-size:20px;line-height:2}
.mp {text-align:justify;background:#fff}
.mp,.mp code,.mp pre,.mp pre code,.mp tt,.mp kbd,.mp samp {color:#131211}
.mp h1,.mp h2,.mp h3,.mp h4 {color:#030201}
.mp u {text-decoration:underline}
.mp code,.mp strong,.mp b {font-weight:bold;color:#131211}
.mp em,.mp var {font-style:italic;color:#232221;text-decoration:none}
.mp a,.mp a:link,.mp a:hover,.mp a code,.mp a pre,.mp a tt,.mp a kbd,.mp a samp {color:#0000ff}
.mp b.man-ref {font-weight:normal;color:#434241}
.mp pre {padding:0 4ex}
.mp pre code {font-weight:normal;color:#434241}
.mp h2+pre,h3+pre {padding-left:0}
ol.man-decor,ol.man-decor li {margin:3px 0 10px 0;padding:0;float:left;width:33%;list-style-type:none;text-transform:uppercase;color:#999;letter-spacing:1px}
ol.man-decor {width:100%}
ol.man-decor li.tl {text-align:left}
ol.man-decor li.tc {text-align:center;letter-spacing:4px}
ol.man-decor li.tr {text-align:right;float:right}
</style>
</head>
<!--
The following styles are deprecated and will be removed at some point:
div#man, div#man ol.man, div#man ol.head, div#man ol.man.
The .man-page, .man-decor, .man-head, .man-foot, .man-title, and
.man-navigation should be used instead.
-->
<body id='manpage'>
<div class='mp' id='man'>
<div class='man-navigation' style='display:none'>
<a href="#NAME">NAME</a>
<a href="#SYNOPSIS">SYNOPSIS</a>
<a href="#DESCRIPTION">DESCRIPTION</a>
<a href="#THE-_RUN_-HELPER">THE _RUN_ HELPER</a>
<a href="#THE-_LOAD_-COMMAND">THE _LOAD_ COMMAND</a>
<a href="#THE-_SKIP_-COMMAND">THE _SKIP_ COMMAND</a>
<a href="#SETUP-AND-TEARDOWN-FUNCTIONS">SETUP AND TEARDOWN FUNCTIONS</a>
<a href="#CODE-OUTSIDE-OF-TEST-CASES">CODE OUTSIDE OF TEST CASES</a>
<a href="#SPECIAL-VARIABLES">SPECIAL VARIABLES</a>
<a href="#SEE-ALSO">SEE ALSO</a>
</div>
<ol class='man-decor man-head man head'>
<li class='tl'>bats(7)</li>
<li class='tc'></li>
<li class='tr'>bats(7)</li>
</ol>
<h2 id="NAME">NAME</h2>
<p class="man-name">
<code>bats</code> - <span class="man-whatis">Bats test file format</span>
</p>
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<pre><code>load test_helper
setup() {
# set up your environment
# run before and after each test case
}
teardown() {
# clean up your environment
# run before and after each test case
}
code_outside_of_test_cases () {
# For example, check for dependencies
# and fail immediatelyif they're not present.
# Output must be redirected to `stderr` (`&gt;&amp;2`)
}
@test "test description" {
run foo arguments
[ "$status" -eq 1 ]
[ "$output" = "expected output" ]
}
@test "test description" {
run foo arguments
[ "$status" -eq 1 ]
[ "${lines[0]}" = "first line of expected output" ]
}
@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 ]
}
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn't bar"
fi
run foo
[ "$status" -eq 0 ]
}
</code></pre>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
<p>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.</p>
<pre><code>#!/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 ]
}
</code></pre>
<p>Each Bats test file is evaulated n+1 times, where <em>n</em> is the number of
test 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 own
process.</p>
<p>For details about exactly how Bats evaluates test files, see
Bats Evaluation Process:
https://github.com/sstephenson/bats/wiki/Bats-Evaluation-Process</p>
<h2 id="THE-_RUN_-HELPER">THE <code>_RUN_</code> HELPER</h2>
<p>Many Bats tests need to run a command and then make assertions about
its exit status and output. Bats includes a <code>run</code> helper that invokes
its arguments as a command, saves the exit status and output into
special global variables, and then returns with a <code>0</code> status code so
you can continue to make assertions in your test case.</p>
<p>For example, let's say you're testing that the <code>foo</code> command, when
passed a nonexistent filename, exits with a <code>1</code> status code and prints
an error message.</p>
<pre><code>@test "invoking foo with a nonexistent file prints an error" {
run foo nonexistent_filename
[ "$status" -eq 1 ]
[ "$output" = "foo: no such file 'nonexistent_filename'" ]
}
</code></pre>
<p>The <code>$status</code> variable contains the status code of the command, and
the <code>$output</code> variable contains the combined contents of the command's
standard output and standard error streams.</p>
<p>A third special variable, the <code>$lines</code> array, is available for easily
accessing individual lines of output. For example, if you want to test
that invoking <code>foo</code> without any arguments prints usage information on
the first line:</p>
<pre><code>@test "invoking foo without arguments prints usage" { run foo
[ "$status" -eq 1 ]
[ "${lines[0]}" = "usage: foo &lt;filename>" ]
}
</code></pre>
<h2 id="THE-_LOAD_-COMMAND">THE <code>_LOAD_</code> COMMAND</h2>
<p>You may want to share common code across multiple test files. Bats
includes a convenient <code>load</code> 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 <code>test/foo.bats</code>, the command</p>
<pre><code>load test_helper
</code></pre>
<p>will source the script <code>test/test_helper.bash</code> in your test file. This
can be useful for sharing functions to set up your environment or load
fixtures.</p>
<h2 id="THE-_SKIP_-COMMAND">THE <code>_SKIP_</code> COMMAND</h2>
<p>Tests can be skipped by using the <code>skip</code> command at the point in a
test you wish to skip.</p>
<pre><code>@test "A test I don't want to execute for now" {
skip
run foo
[ "$status" -eq 0 ]
}
</code></pre>
<p>Optionally, you may include a reason for skipping:</p>
<pre><code>@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 ]
}
</code></pre>
<p>Or you can skip conditionally:</p>
<pre><code>@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn't bar"
fi
run foo
[ "$status" -eq 0 ]
}
</code></pre>
<h2 id="SETUP-AND-TEARDOWN-FUNCTIONS">SETUP AND TEARDOWN FUNCTIONS</h2>
<p>You can define special <code>setup</code> and <code>teardown</code> 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.</p>
<h2 id="CODE-OUTSIDE-OF-TEST-CASES">CODE OUTSIDE OF TEST CASES</h2>
<p>You can include code in your test file outside of <code>@test</code> functions.
For example, this may be useful if you want to check for dependencies
and fail immediately if they're not present. However, any output that
you print in code outside of <code>@test</code>, <code>setup</code> or <code>teardown</code> functions
must be redirected to <code>stderr</code> (<code>&gt;&amp;2</code>). Otherwise, the output may
cause Bats to fail by polluting the TAP stream on <code>stdout</code>.</p>
<h2 id="SPECIAL-VARIABLES">SPECIAL VARIABLES</h2>
<p>There are several global variables you can use to introspect on Bats
tests:</p>
<ul>
<li><code>$BATS_TEST_FILENAME</code> is the fully expanded path to the Bats test
file.</li>
<li><code>$BATS_TEST_DIRNAME</code> is the directory in which the Bats test file is
located.</li>
<li><code>$BATS_TEST_NAMES</code> is an array of function names for each test case.</li>
<li><code>$BATS_TEST_NAME</code> is the name of the function containing the current
test case.</li>
<li><code>$BATS_TEST_DESCRIPTION</code> is the description of the current test
case.</li>
<li><code>$BATS_TEST_NUMBER</code> is the (1-based) index of the current test case
in the test file.</li>
<li><code>$BATS_TMPDIR</code> is the location to a directory that may be used to
store temporary files.</li>
</ul>
<h2 id="SEE-ALSO">SEE ALSO</h2>
<p><a class="man-ref" href="bats.1.html">bats<span class="s">(1)</span></a></p>
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>November 2013</li>
<li class='tr'>bats(7)</li>
</ol>
</div>
</body>
</html>

View File

@ -1,58 +1,6 @@
bats(7) -- Bats test file format bats(7) -- Bats test file format
================================ ================================
SYNOPSIS
--------
load test_helper
setup() {
# set up your environment
# run before and after each test case
}
teardown() {
# clean up your environment
# run before and after each test case
}
code_outside_of_test_cases () {
# For example, check for dependencies
# and fail immediatelyif they're not present.
# Output must be redirected to `stderr` (`>&2`)
}
@test "test description" {
run foo arguments
[ "$status" -eq 1 ]
[ "$output" = "expected output" ]
}
@test "test description" {
run foo arguments
[ "$status" -eq 1 ]
[ "${lines[0]}" = "first line of expected output" ]
}
@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 ]
}
@test "A test which should run" {
if [ foo != bar ]; then
skip "foo isn't bar"
fi
run foo
[ "$status" -eq 0 ]
}
DESCRIPTION DESCRIPTION
----------- -----------
@ -79,13 +27,9 @@ test 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 own then iterates over the test cases and executes each one in its own
process. process.
For details about exactly how Bats evaluates test files, see
Bats Evaluation Process:
https://github.com/sstephenson/bats/wiki/Bats-Evaluation-Process
THE RUN HELPER
THE `_RUN_` HELPER --------------
------------------
Many Bats tests need to run a command and then make assertions about Many Bats tests need to run a command and then make assertions about
its exit status and output. Bats includes a `run` helper that invokes its exit status and output. Bats includes a `run` helper that invokes
@ -112,14 +56,15 @@ accessing individual lines of output. For example, if you want to test
that invoking `foo` without any arguments prints usage information on that invoking `foo` without any arguments prints usage information on
the first line: the first line:
@test "invoking foo without arguments prints usage" { run foo @test "invoking foo without arguments prints usage" {
run foo
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
[ "${lines[0]}" = "usage: foo <filename>" ] [ "${lines[0]}" = "usage: foo <filename>" ]
} }
THE `_LOAD_` COMMAND THE LOAD COMMAND
------------------ ----------------
You may want to share common code across multiple test files. Bats You may want to share common code across multiple test files. Bats
includes a convenient `load` command for sourcing a Bash source file includes a convenient `load` command for sourcing a Bash source file
@ -133,8 +78,8 @@ can be useful for sharing functions to set up your environment or load
fixtures. fixtures.
THE `_SKIP_` COMMAND THE SKIP COMMAND
------------------ ----------------
Tests can be skipped by using the `skip` command at the point in a Tests can be skipped by using the `skip` command at the point in a
test you wish to skip. test you wish to skip.
@ -208,4 +153,4 @@ store temporary files.
SEE ALSO SEE ALSO
-------- --------
bats(1) `bash`(1), `bats`(1)

View File

@ -1,3 +0,0 @@
# manuals
bats(1) bats.1.ronn
bats(7) bats.7.ronn