Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 29, 2021. It is now read-only.
/batsPublic archive

Bash Automated Testing System

License

NotificationsYou must be signed in to change notification settings

sstephenson/bats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

Running tests

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

Test suites

You can invoke thebats interpreter with multiple test filearguments, or with a path to a directory containing multiple.batsfiles. Bats will run each test file individually and aggregate theresults. If any test case fails,bats exits with a1 status code.

Writing tests

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.

run: Test other commands

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>" ]}

load: Share common code

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.

skip: Easily skip tests

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 ]}

setup andteardown: Pre- and post-test hooks

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.

Code outside of test cases

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.

Special variables

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.

Installing Bats from source

Check out a copy of the Bats repository. Then, either add the Batsbin directory to your$PATH, or run the providedinstall.shcommand 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.

Support

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.

Version history

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 theload helper.
  • Added support for single-line@test definitions.
  • Added bats(1) and bats(7) manual pages.
  • Modified thebats command to default to TAP output when the$CIvariable 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 theskip 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 invokebats 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. Thebats 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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp