[TIP] Run-time configuration of unittest cases

Mateusz Łoskot mateusz at loskot.net
Wed Apr 23 14:44:46 PDT 2014

On 23 April 2014 22:44, John Wong <gokoproject at gmail.com> wrote:
> On Wed, Apr 23, 2014 at 4:32 PM, Mateusz Łoskot <mateusz at loskot.net> wrote:
>> 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.
> What kind of parameters? Are you expecting something like
> test1 --> read data from /location/folder1
> test2 --> read data from /newlocation/somedub/folder2

Yes, as I wrote the parameters are test data location, connection
details to test databases, etc.
So, also

test3 -> read data from "dbname=x user=y password=z"

> I normally go with configuration files. I like to have locality; the easier
> I can inspect and edit values, the better.

I second that.

> Also, I'd like to keep them in version control. If I already have a shell script that takes a set of
> parameters into os.env then I will expect that to be in version control.

I do something similar.
I like to keep, let's say, default configuration (as there is one) in
VCS, e.g. test.conf,
but also allow to override defaults with test.conf.local which is not in VCS,
but serves as per-user custom configuration.
The test.conf.local, if present, is preferred by config reader.

My question though is more ways to pass parameters, read from file
or read from os.env, to unittest.TestCase instances.
For example, this looks fine:

import unittest
import myconfig

config = {}
def setUpModule():
    config = myconfig.read() # raises if no config found

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

More information about the testing-in-python mailing list