[TIP] Run-time configuration of unittest cases
mateusz at loskot.net
Wed Apr 23 12:54:12 PDT 2014
I have a collection of integration or system tests based on unittest package.
I'd like to sample for ideas about how to pass a set of parameters to
unittest.TestCase objects in order to configure them at run-time
with test data location, connection details to test databases, etc.
I found Eli's solution to parametrize test cases , which uses
staticmethod as a kind of a named constructor.
However, it requires explicit test case class selection and
creation of unittest.TestSuite instance.
I found some other solutions like this  based on class variables
initialised with data passed in sys.argv.
Similarly, global variables could be used.
I'd like to be able to write tests simply like this:
if __name__ == '__main__':
I think a viable alternative is to maintain a single tests.conf file with
all parameters stored.
In every test module with test case that require any run-time configuration,
run-time parameters are read from tests.conf file in setUpModule into
a global dictionary.
Finally, TestCase.setUp accesses those parameters.
I was thinking about replacing test.conf with Python module imported
in every test module, but that seems to be a variation of the test.conf
option above. It also wouldn't benefit from the fact that no tests are run
if setUpModule fails to fetch required parameters.
How do you achieve configuration of integration tests?
Any better solutions out there?
BTW, I don't want to switch to py.test because I can't rely on
just Python 3.2+ and unittest.
Mateusz Łoskot, http://mateusz.loskot.net
More information about the testing-in-python