[TIP] Run-time configuration of unittest cases

Mateusz Łoskot mateusz at loskot.net
Wed Apr 23 12:54:12 PDT 2014


Hi,

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 [1], 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 [2] 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:

import unittest
class TestX(unittest.TestCase):
    def setUp(self):
          ...
    def test_a(self):
          ...
if __name__ == '__main__':
    unittest.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
thirdparty modules,
just Python 3.2+ and unittest.


[1] http://eli.thegreenplace.net/2011/08/02/python-unit-testing-parametrized-test-cases/
[2] http://stackoverflow.com/a/20702984/151641

Best regards,
-- 
Mateusz  Łoskot, http://mateusz.loskot.net



More information about the testing-in-python mailing list