Loading and running Python tests from code
Loading and running tests from code is possible, but it is not immediately clear from the docs of unittest. This is how I did it:
In the project which I’m currently involved with I’m using an early version of a simulator for connected devices. The first version of the simulator used a rather hairy bash script to set up environment variables, launch a web interface, load the simulator and run the specified simulation. Since the simulator is mostly written in Python though, I wanted to remove the dependencies on bash and also the cryptic environment variables.
Python has an awesome library called argparse. Using argparse and some regular Python magic I substituted the original bash script with a Python one both reducing code size (by half) and increasing readability.
Additionally, I created a set of sub-commands to run and test simulations using the code above. Now it is only a matter of running:
./simulator test projectname