Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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