对所有鼻子测试进行一次设置和拆卸功能 [英] Setup and teardown functions executed once for all nosetests tests
问题描述
如何对所有的鼻子测试进行一次设置和拆卸功能?
How to execute setup and teardown functions 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()
Note that there exists a similar question with an answer that is not working with python 2.7.9-1, python-unittest2 0.5.1-1 and python-nose 1.3.6-1 after replacing the dots with pass
and adding the line import unittest
.
Unfortunately my reputation is too low to comment on that.
推荐答案
您可以具有模块级设置功能.根据鼻子文档:
You can have a module level setup function. According to nose documentation:
测试模块提供模块级别的设置和拆卸;定义方法 设置, setup_module ,setUp或setUpModule进行设置,拆卸, teardown_module ,或使用tearDownModule进行拆卸.
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.
因此,更具体地说,针对您的情况:
So, more specifically, for your case:
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
更加清晰.
It does not matter which name you choose, so both setup
and setup_module
would work the same, but setup_module
has more clarity to it.
这篇关于对所有鼻子测试进行一次设置和拆卸功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!