简体   繁体   English

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

[英]How to define a setup method only called once during testing with nosetest?

I am running a couple of nosetests with test cases in different modules (files) each containing different tests. 我跑了几个nosetests在不同的模块(文件)的测试情形分别包含不同的测试。

I want to define a function/method that is only called once during the execution with nosetest . 我想定义一个函数/方法,只在执行nosetest期间调用一次。

I looked at the documentation (and eg here ) and see there are methods like setup_module etc. - but where and how to use them? 我查看了文档 (例如这里 )并看到有类似setup_module等的方法 - 但是在哪里以及如何使用它们? Put them into my __init__.py ? 把它们放进我的__init__.py Something else? 别的什么?

I tried to use the following: 我试着使用以下内容:

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

    ...

but this printout is never done when I run the test with nosetest . 但是当我用nosetest运行测试时,这个打印输出永远不会完成。 I also do not derive from unittest.TestCase (which will result in errors). 我也不是从unittest.TestCase派生的(这将导致错误)。

When looking at a package level, you can define a function named setup in the __init__.py of that package. 查看包级别时,可以在该包的__init__.py中定义名为setup的函数。 Calling the tests in this package, the setup function in the __init__.py is called once. 调用此包中的测试, __init__.pysetup函数被调用一次。

Example setup 示例设置

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

See documentation section 'Test packages'. 请参阅文档部分“测试包”。

Try this one 试试这个吧

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 it helps ^^ Holp它有助于^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM