Creating a simple testing script
We now have a script with a command line interface that allows people to use the code without having to read the source. At this point we are feeling rather professional and are happy to share the code with others. One of the things that is often scary is the thought of others finding bugs in our code and then having to fix these bugs. A good way to alleviate these fears is to do some testing of the code ourselves to catch all the obvious errors before the script is sent to others. In a later lesson we’ll go through some more formal and rigorous testing, but for now we are going to start simple with some of the most basic tests.
In our file test_sim.sh
we will make three tests. The first two tests we are going to do will just ensure that the code will not immediately crash when we run it:
#! /usr/bin/env bash
echo "Testing sim.py"
python sim.py || { echo "FAILED to run with default parameters"; exit 1 ;}
python sim.py --help || { echo "FAILED to print help"; exit 1 ;}
The next test we do will ensure that when we pass a filename to the --out
option, we will get a new file with that name. We use the bash [ ]
test and -f
conditional to see that the file exists.
python sim.py --out test.csv
if [ ! -f "test.csv" ]; then
echo "FAILED to generate ouput test.csv"
exit 1
fi
echo "all tests PASSED"
exit 0
The last two lines above then make sure that when the tests all pass we get a positive message and return an exit status of 0.
We now have a test script that will catch the most egregious issues with our script. Any time we make changes to our script we should run the test script just to make sure that we haven’t seriously broken anything.
More advanced testing with bash is not explored here as we will move to a python based test environment in a later lesson. The python based testing will make it easier to do things like make sure the right number of entries exist in the catalogue file and that the positions all lie within the specified region.