如何为所有鼻子测试执行一次设置和拆卸功能?
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()
Run Code Online (Sandbox Code Playgroud)
请注意,在将点替换为并添加行后,存在一个类似的问题,其答案不适用于 python 2.7.9-1、python-unittest2 0.5.1-1 和 python-nose 1.3.6-1 。不幸的是,我的声誉太低,无法对此发表评论。passimport unittest
您可以拥有模块级别的设置功能。根据鼻子文件:
测试模块提供模块级设置和拆卸;定义方法 setup、setup_module、setUp 或 setUpModule 用于设置、teardown、 teardown_module或 tearDownModule 用于拆卸。
因此,更具体地说,对于您的情况:
def setup_module():
print "common_setup"
def teardown_module():
print "common_teardown"
def test_1():
print "test_1"
def test_2():
print "test_2"
Run Code Online (Sandbox Code Playgroud)
运行测试为您提供:
$ 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
Run Code Online (Sandbox Code Playgroud)
这不要紧,你选择哪一个名字,所以无论setup和setup_module会的工作相同,但setup_module具有更清晰吧。
| 归档时间: |
|
| 查看次数: |
1583 次 |
| 最近记录: |