为所有 nosetests 测试执行一次设置和拆卸功能
Setup and teardown functions executed once for all nosetests tests
如何为所有 nosetests 测试执行一次设置和拆卸功能?
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
替换点并添加行 import 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 测试执行一次设置和拆卸功能?
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
替换点并添加行 import 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
更清晰。