编程语言
首页 > 编程语言> > python-所有鼻子测试的安装和拆卸功能都执行一次

python-所有鼻子测试的安装和拆卸功能都执行一次

作者:互联网

如何对所有鼻子测试进行一次设置和拆卸功能?

def common_setup():
    #time consuming code
    pass

def common_teardown():
    #tidy up
    pass

def test_1():
    pass

def test_2():
    pass

#desired behavior
common_setup()
test_1()
test_2()
common_teardown()

请注意,存在一个similar question,其答案不适用于python 2.7.9-1,python-unittest2 0.5.1-1和python-nose 1.3.6-1,之后将点替换为pass并添加了行导入unittest .
不幸的是,我的声誉太低,无法对此发表评论.

解决方法:

您可以具有模块级设置功能.根据nose documentation

Test modules offer module-level setup and teardown; define the method
setup, setup_module, setUp or setUpModule for setup, teardown,
teardown_module, or tearDownModule for teardown.

因此,更具体地说,针对您的情况:

def setup_module():
    print "common_setup"

def teardown_module():
    print "common_teardown"

def test_1():
    print "test_1"

def test_2():
    print "test_2"

运行测试为您提供:

$nosetests common_setup_test.py -s -v
common_setup
common_setup_test.test_1 ... test_1
ok
common_setup_test.test_2 ... test_2
ok
common_teardown

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

选择哪个名称都没有关系,因此setup和setup_module的工作原理相同,但是setup_module更加清晰.

标签:nosetests,python
来源: https://codeday.me/bug/20191120/2042011.html