[TIP] Using one unit test with different input parameters

Pete pfein at pobox.com
Tue Feb 19 10:59:40 PST 2008

On Tuesday February 19 2008 1:18:14 pm Robert McHardy wrote:
> The client has 28 sites that are very similar.  I'd rather not have
> to clone the test script 28 times with different hard-coded domain
> details...  but I can't figure out how to pass in different
> parameters to the unit test - e.g. a different DOMAIN dict with data
> for other sites, and have the tests run on that domain instead.

This sounds really similar to a problem/solution I posted a few months back on 
the nose list I called "Generic Tests".  Basically, my idea was to have a 
factory_tests() function that returned TestCase classes derriving from a 
BaseTest that did the actual work.  Some code might make more sense:

def factory_test(domain_dict):
   class _TestCase(Site_BaseTest):
       def test_it():
           assert do_stuff(cls.domain_dict['DOMAIN'])
   return _TestCase

and then in your test module, you do factory_test(dd for dd in 
list_of_domain_dicts).  You'll need to get the resulting classes executed by 
your test harness tho - for nose, that consisted of giving them unique, 
nose-friendly names.  If you're using unittest, there's some way of 
explicitly running TestCases that I don't know off the top of my head.

Has anyone else used this technique?  Got a name for it?  My use case was 
testing different implmenations of a public API, but the approach seems like 
it could be useful in a bunch of cases.

Peter Fein   ||   773-575-0694   ||   pfein at pobox.com
http://www.pobox.com/~pfein/   ||   PGP: 0xCCF6AE6B
irc: pfein at freenode.net   ||   jabber: peter.fein at gmail.com

More information about the testing-in-python mailing list