课堂内的鼻子测试发生器

发布于 2024-11-23 19:57:44 字数 775 浏览 1 评论 0原文

是否可以在自定义类中运行鼻子测试生成器?我正在尝试将 示例 转换为基于简单类的版本:

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

我哪里出错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-11-30 19:57:45

我相信你必须子类化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

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
星軌x 2024-11-30 19:57:44

解决方案是一个不太预期的解决方案:为了让nosetests发现生成器方法,不要从unittest.TestCase子类化。使用nosetests 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文