nick_gabpe
nick_gabpe

Reputation: 5805

Nose generate dynamically test with decorator

I have dynamic amount of test so i want use for loop I'd try something like:

from nose.tools import istest, nottest
from nose.tools import eq_
import nose
nose.run()
@istest
def test_1():
    for i in range(100):
        @istest
        def test_1_1():
            eq_(randint(1,1),1)



---------------------
Ran 1 test in 0.001s

OK

But nose display it like only one test. How can i improve it to 100 tests? Thanks in advance.

Upvotes: 1

Views: 141

Answers (1)

wim
wim

Reputation: 363294

For data-driven tests in nose, check out nose_parameterized.

Example usage:

from nose_parameterized import parameterized

@parameterized.expand([(1, 1, 2), (2, 2, 4)])
def test_add(self, a, b, sum):
    self.assertEqual(sum, a + b)

Here, two tests will be generated by the runner. It tests 1+1==2 and 2+2==4. The decorator is also compatible with other test runners such as unittest.

Upvotes: 2

Related Questions