如何在使用nosetest测试期间定义仅调用一次的设置方法?

问题描述 投票:5回答:2

我正在运行几个nosetests,其中包含不同模块(文件)中的测试用例,每个模块包含不同的测试。

我想定义一个在nosetest执行期间只调用一次的函数/方法。

我查看了documentation(例如here)并看到有像setup_module等方法 - 但在哪里以及如何使用它们?把它们放进我的__init__.py?别的什么?

我试着使用以下内容:

class TestSuite(basicsuite.BasicSuite):
    def setup_module(self):
        print("MODULE")

    ...

但是当我用nosetest运行测试时,这个打印输出永远不会完成。我也不是从unittest.TestCase派生的(会导致错误)。

python nose
2个回答
9
投票

查看包级别时,可以在该包的setup中定义名为__init__.py的函数。调用此包中的测试,setup中的__init__.py函数被调用一次。

示例设置

- package
    - __init__.py
    - test1.py
    - test2.py

请参阅documentation部分'测试包'。


1
投票

试试这个吧

from nose import with_setup

def my_setup_function():
    print ("my_setup_function")

def my_teardown_function():
    print ("my_teardown_function")

@with_setup(my_setup_function, my_teardown_function)
def test_my_cool_test():
    assert my_function() == 'expected_result'

Holp它有助于^^

© www.soinside.com 2019 - 2024. All rights reserved.