未为测试生成器执行 Python nose 设置/拆卸类夹具方法

标签 python nose

在一个业余项目中,我打算使用 nose 进行测试,我想将特定类的所有测试放入类中,因为这些测试共享设置和其他功能。但我似乎无法在类中执行设置方法。

这是一个经过测试的示例类:

class mwe():
    def __init__(self):
        self.example = ""
    def setExample(self, ex):
        self.example = ex

当我不使用类时,测试有效:

from nose.tools import ok_
import mwe

exampleList = []

def setUp():
    print("setup")
    exampleList.append("1")
    exampleList.append("2")
    exampleList.append("3")

def test_Example():
    print("test")
    for ex in exampleList:
        t = mwe.mwe()
        t.setExample(ex)
        yield check, t, ex

def check(e, ex):
    ok_(e.example == ex)

输出符合预期:

setup
test
...
----------------------------------------------------------------------
Ran 3 tests in 0.004s

OK

当使用测试类时,不会执行设置方法,因此不会执行任何测试。

from nose.tools import ok_
import mwe

class TestexampleClass(object):

    def __init__(self):
        print("__init__")
        self.exampleList = []

    def setup(self):
        print("setup class")
        self.exampleList.append("1")
        self.exampleList.append("2")
        self.exampleList.append("3")   

    def test_ExampleClass(self):
        print("test class")
        for ex in self.exampleList:
            t = mwe.mwe()
            t.setExample(ex)
            yield self.check, t, ex

    def check(self, we, ex):
        print("check class")
        ok_(we.example == ex)

我是 python 的新手,也是一个有 Nose 的新手,我的问题是,为什么没有执行设置?我的代码哪里出错了?

__init__
test class

----------------------------------------------------------------------
Ran 0 tests in 0.002s

OK

我会很高兴收到任何反馈。

当我使用此 Question on SO 中的代码时正如我所期望的那样,setup 方法被执行了。

解决方案: 在万般无奈之后,我发现了以下内容: Nose 在执行 yield 函数之前执行类级设置方法,而不是在调用 test_* 方法时执行,正如我预期的那样,其他 test_* 方法。这显然违背了 Nose 文档:

Setup and teardown functions may be used with test generators. However, please note that setup and teardown attributes attached to the generator function will execute only once. To execute fixtures for each yielded test, attach the setup and teardown attributes to the function that is yielded, or yield a callable object instance with setup and teardown attributes.

查看错误报告,我发现 the bug report on github.

一个可能的解决方法是使用类级别的固定装置:

@classmethod
def setup_class(cls):
    #do stuff
    pass

最佳答案

您的测试类需要扩展 TestCase 并且需要调用设置方法 setUp

from unittest import TestCase

class TestUtils(TestCase):
    def setUp(self):
        self.x = 1

    def test_something(self):
        self.assertEqual(1, self.x)

哪些输出

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

关于未为测试生成器执行 Python nose 设置/拆卸类夹具方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888583/

相关文章:

python - 优化 Python 中 numpy 数组中元素的索引和检索?

python - 如何处理 sqlalchemy+psycopg2 中不断变化的密码?

python - 我在连接 mongodb Atlas : "dns.exception.Timeout: The DNS operation timed out after 30.000985383987427 seconds" 时收到此错误

python - 糟糕的 Django 管理性能

python - nosetests 做另一个输出 (ImportError) 然后简单的单元测试 (No Error) 为什么?

Python 连续运行单元测试或每个测试多次

python - python compile_command 是否考虑环境?

python - 使用 Nose 和 Sqlalchemy 以及固定装置编写 Python 测试类

python - python 中的部分回车

python - 是否可以从 nose 插件访问类级属性?