gpt4 book ai didi

python - 类内的 Nose 测试生成器

转载 作者:IT老高 更新时间:2023-10-28 20:53:23 25 4
gpt4 key购买 nike

是否可以在自定义类中运行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com