gpt4 book ai didi

python - 为所有 nosetests 测试执行一次设置和拆卸功能

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:56 28 4
gpt4 key购买 nike

如何为所有 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在用 pass 替换点并添加行 导入单元测试。不幸的是,我的声誉太低,无法对此发表评论。

最佳答案

您可以拥有模块级别的设置功能。根据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

选择哪个名称并不重要,因此 setupsetup_module 的工作方式相同,但 setup_module 更清晰.

关于python - 为所有 nosetests 测试执行一次设置和拆卸功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750621/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com