Unit tests in Nix
Create a file, name it test.bats
:
#!/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 ]
}
@test "Ant is installed" {
ant -version
}
Make it executable: chmod +x test.bats
.
Run the test in nix-shell
:
$ nix-shell -p bats -p ant --run ./test.bats
✓ addition using bc
✓ addition using dc
✓ Ant is installed
3 tests, 0 failures
At least for availability of packages, you can now test this way!
And if bats
itself is not available ‒ well, then you will also get an error from your tests ‒ which is very good!
If at some time you want to parallelize your test execution, add a -j [N_JOBS]
parameter to the call (requires GNU Parallel).
For example, on a machine with 8 CPU cores, that could become:
$ nix-shell -p bats -p ant --run "./test.bats -j 8"