Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Bats - Bash Automated Testing System: Difference between revisions

From John's Wiki
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 edit summary
 
Line 7: Line 7:
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.


<pre>#!/usr/bin/env bats
<syntaxhighlight lang="bash" line>
#!/usr/bin/env bats


@test &quot;addition using bc&quot; {
@test &quot;addition using bc&quot; {
   result=&quot;$(echo 2+2 | bc)&quot;
   result=&quot;$(echo 2+2 | bc)&quot;
   [ &quot;$result&quot; -eq 4 ]
   [ &quot;$result&quot; -eq 4 ]
}</pre>
}
</syntaxhighlight>


(The above overview section was stolen verbatim from [https://github.com/sstephenson/bats the main projects README]).
(The above overview section was stolen verbatim from [https://github.com/sstephenson/bats the main projects README]).
Line 35: Line 37:
<code>test.bats</code>
<code>test.bats</code>


<pre>
<syntaxhighlight lang="bash" line>
#!/usr/bin/env bats
#!/usr/bin/env bats


Line 51: Line 53:
assert_equal "$(basename $BATS_TEST_FILENAME)" 'test.bats'
assert_equal "$(basename $BATS_TEST_FILENAME)" 'test.bats'
}
}
</pre>
</syntaxhighlight>


=== Running the First Tests ===
=== Running the First Tests ===

Latest revision as of 21:39, 28 November 2024

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 &quot;addition using bc&quot; {
  result=&quot;$(echo 2+2 | bc)&quot;
  [ &quot;$result&quot; -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.

Main / Original Project README

Bats Assert README

Current / Bats Support Project Readme