shUnit2 is the great-grandmother of all bash test frameworks. I really like that the test files are plain old Bash files, and that it has lot's of assertions by default. However, regarding other features it seems to be lagging behind it's competitors.
If you are after a testing framework where you can write tests in pure Bash scripts, make sure to also check bash_unit.
✔️ Test files are simple Bash scripts that one can execute. Functions are picked up by the test framework based on a naming convention, and overall the look and feel of a test file is very similar to JUnit.
testEquality() {
assertEquals 1 1
}
. ./shunit2
It's easy to get up and running with a simple wrapper, but things can easily get more complicated if you'd like to support proper test file discovery including subdirectories, partial execution, and executing all test files regardless of failures but still emit appropriate status code to indicate failures.
Related: kward/shunit/issues/52
It has the concept of a test suite, but only in a single file. It can take test function names as an argument to focus on some dedicated tests and only execute those. It does not support wildcard matching.
✔️ Among the frameworks I've checked, shUnit2 has the most assertions available by default:
assertEquals
assertNotEquals
assertContains
assertNotContains
assertNull
assertNotNull
assertTrue
assertFalse
(In fact, there might be a bit too many of them, some providing similar functionalities like assertEquals
and assertSame
.)
✔️ They can also take optional messages that will be displayed upon failures, which is quite nice.
${_ASSERT_EQUALS_} '"not equal"' 2 2
Using macros involve quite some line noise, and it's also a bit problematic that with this test files might become mixed, using both assertion functions and macros for the same reason.
✔️ It’s possible to create new assertions by building functions on top of the existing assertions, although there's no dedicated no dedicated lower-level API to support it. Custom assertions can be sourced from an external file.
✔️ shUnit2 has an unique approach to skipping tests: instead of skipping whole test cases, here it's possible to skip certain assertions:
assertEquals 1 1 # This assertion will run
startSkipping
assertEquals 1 1 # This assertion will be skipped
endSkipping
assertEquals 1 1 # This assertion will also run
If the test starts with startsSkipping
, and there are no stopSkipping
the whole test will be skipped.
This can be handy to temporarily disable some faulty assertions in a test.
testEquality1
testEquality2
Ran 2 tests.
OK (skipped=1)
# Which one was skipped? :(
✔️ The example tests make it very clear how to use the framework, and the project's readme does not leave out a single feature.