[TIP] Run-time configuration of unittest cases

Andres Riancho andres.riancho at gmail.com
Wed Apr 23 13:16:52 PDT 2014


Usually I just set the environment variables in the CI system, and in
the python source code do:

import os
os.getenv('VARIABLE')

That's dead-simple and works :)

CI systems support setting environment variables, and if developers
want to run some of those tests they can always do it like:

user at console$ VARIABLE=1 nosetests test_foo.py

Or if you've got many variables:

user at console$ export VARIABLE=1
user at console$ export VARIABLE_N=23
user at console$ nosetests test_foo.py

On Wed, Apr 23, 2014 at 4:54 PM, Mateusz Łoskot <mateusz at loskot.net> wrote:
> 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
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python



-- 
Andrés Riancho
Project Leader at w3af - http://w3af.org/
Web Application Attack and Audit Framework
Twitter: @w3af
GPG: 0x93C344F3



More information about the testing-in-python mailing list