Search code examples
pythongeneratornose

Nose Test Generators inside Class


Is it possible to run nose test generators inside custom classes? I am trying to convert the example into a simple class based version:

file: trial.py
>>>>>>>>>>>>>>
class ATest():
    def test_evens(self):
        for i in range(0, 5):
            yield self.check_even, i, i * 3

    def check_even(self, n, nn):
        assert n % 2 == 0 or nn % 2 == 0

That results in

$ nosetests -v trial.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s

I had a look through the changelog and believe that this should work since version 0.9.0a1.

Where am I going wrong?


Solution

  • The solution is the less expected one: do NOT subclass from unittest.TestCase in order to have nosetests discover the generator method. Code working with nosetests 1.1.3 (latest from GitHub):

    class TestA(object):
        def test_evens(self):
            for i in range(0, 5):
                yield self.check_even, i, i * 3
    
        def check_even(self, n, nn):
            assert n % 2 == 0 or nn % 2 == 0
    

    Also, use TestA instead of ATest.

    test.py:2: TestA.test_evens[0] PASSED
    test.py:2: TestA.test_evens[1] FAILED
    test.py:2: TestA.test_evens[2] PASSED
    test.py:2: TestA.test_evens[3] FAILED
    test.py:2: TestA.test_evens[4] PASSED