Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nose Test Generators inside Class

Tags:

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?

like image 660
Longestline Avatar asked Jul 14 '11 06:07

Longestline


1 Answers

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
like image 105
andres.riancho Avatar answered Sep 18 '22 19:09

andres.riancho