开发者

Nose Test Generators inside Class

开发者 https://www.devze.com 2023-03-20 17:24 出处:网络
Is it possible to run nose test generators inside custom classes? I am trying to convert the example into a simple class based version:

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):
      开发者_StackOverflow社区  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?


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


I believe you have to subclass unittest.TestCase

import unittest

class ATest(unittest.TestCase):
    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
0

精彩评论

暂无评论...
验证码 换一张
取 消