3

I have a list of twelve properties and want to test various combinations. The actual test function is the same, just different parameters. I find myself writing a bunch of functions that look like this:

def test_paramA(self): self.with_params("A")
def test_paramA_B(self): self.with_params("A+B")
def test_paramA_C(self): self.with_params("A+C")
def test_paramB_C(self): self.with_params("B+C")

I'd like to instead write this:

combinations = ["A", "A+B", "A+C", "B+C"]
combinationSuite = (make_test_function(c) for c in combinations)

Is there any way to make my test driver (nosetests) run all the tests in combinationSuite? Do I need a custom test collector?

3

Nose supports parametric/generative tests, http://readthedocs.org/docs/nose/en/latest/writing_tests.html#test-generators

For example:

def checker(combination):
    # do something to verify the combination 
    assert 'A' in combination

def test_generator():
    options = ['A', 'B', 'C']
    for o in options:
        yield checker, o

    for p in itertools.permutations(options, 2):
        yield checker, p

9 tests are created by nose and results in 4 failures, which would be expected. Also, beware that generative tests do not work with unittest.TestCase subclasses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.