python - 类内的 Nose 测试生成器

标签 python generator nose

是否可以在自定义类中运行 Nose 测试生成器?我正在尝试转换 example变成一个简单的基于类的版本:

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

结果是

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

我查看了更改日志,并认为这应该从版本 0.9.0a1 开始工作。 .

我哪里错了?

最佳答案

解决方案是不太预期的解决方案:不要从 unittest.TestCase 子类化,以便让 Nose 测试发现生成器方法。使用 Nose 测试 1.1.3 的代码(来自 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

另外,使用 TestA 代替 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

关于python - 类内的 Nose 测试生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6689537/

相关文章:

python - 多线程 urllib2 在 Nose 框架上卡住

python - 如何禁用 Nose 测试的覆盖率报告

python - 从字符串中提取列表

python - 流遇到 HTTP 错误 : 403 Twitter API, 但我已提升访问权限

python - 使用 pyzmq 通过 Zeromq 套接字快速传输 Python 字典和列表有哪些选项?

python - 'yield all the output from a generator' 有简写吗?

php - 将生成器分成 block 的最佳方法

python - 如何在 Python 中向模拟函数提供条件参数?

python - 我可以在 Python 中使用类而不是带有映射的函数吗?

java - 随 secret 码生成器返回错误字符 - java