More actions
Created page with "= Bash Automated Testing System = == Overview == Bats is a [https://testanything.org/ TAP-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. <pre>#!/usr/bin/env bats @test "addition using bc" { result="$(echo 2+2 | bc)" [..." |
m Admin moved page Editing Bats - Bash Automated Testing System to Bats - Bash Automated Testing System: Misspelled title: Goofed when copying source from og wiki. |
(No difference)
|
Latest revision as of 11:11, 7 October 2023
Bash Automated Testing System
Overview
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.
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.
#!/usr/bin/env bats @test "addition using bc" { result="$(echo 2+2 | bc)" [ "$result" -eq 4 ] }
(The above overview section was stolen verbatim from the main projects README).
Install
Installing Locally
The steps below are to install bats at a user level (ie. ~/bin ~/.local/lib). However, Bats can also be installed at a system level (ie. root install, /usr/local) or as a submodule of a pre-existing git repository for CI purposes.
Run the commands below to install bats as well as the bats-assert helper library.
mkdir $HOME/.local/lib/bats git clone https://github.com/ztombol/bats-support $HOME/.local/lib/bats/batscore git clone https://github.com/ztombol/bats-assert $HOME/.local/lib/bats/batsassert git clone --depth 1 https://github.com/sstephenson/bats $HOME/bats cd $HOME/bats && ./install.sh $HOME/ && cd .. && rm -rf bats
First Tests
The examples below are two very simple bats test cases. They test whether or not the script's own file name exists and if the file name is 'test.bats' (two tests it should always pass).
test.bats
#!/usr/bin/env bats setup() { # Import core and assert modules. load $HOME/.local/lib/bats/batscore/load.bash load $HOME/.local/lib/bats/batsassert/load.bash } @test "I exist," { [ -e $BATS_TEST_FILENAME ] } @test 'Therefore I am!' { assert_equal "$(basename $BATS_TEST_FILENAME)" 'test.bats' }
Running the First Tests
./test.bats ✓ I exist! ✓ Therefore I am! 2 tests, 0 failures
Continued Documentation
This project is documented pretty well in the following locations. The below docs are also responsible for a lot of the content presented above.
Pick up where this note leaves off by Reading the Docs on Bats.