![](/img/trans.png)
[英]How to call setup once for all tests and teardown after all are finished
[英]Setup and teardown functions executed once for all nosetests tests
如何对所有测试测试执行一次设置和拆卸功能?
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()
请注意,在使用pass
替换点并添加行import unittest
后,存在一个类似的问题 ,答案是不能使用python 2.7.9-1,python-unittest2 0.5.1-1和python-nose 1.3.6-1 import unittest
。 不幸的是,我的名声太低,无法评论。
您可以拥有模块级别设置功能。 根据鼻子文件 :
测试模块提供模块级设置和拆卸; 为setup,teardown, teardown_module或tearDownModule定义方法设置, setup_module ,setUp或setUpModule以进行拆卸。
所以,更具体地说,对于你的情况:
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
更清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.