对所有nosetests测试执行一次Setup和teardown函数

2024-05-15 02:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何对所有的nosetests测试执行一次setup和teardown函数?在

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之后,存在一个similar question的答案,该答案不适用于python 2.7.9-1、python-unittest2 0.5.1-1和python nose 1.3.6-1。 不幸的是,我的名声太低,无法对此发表评论。在


Tags: 函数答案testtimedefsetupcodepass
1条回答
网友
1楼 · 发布于 2024-05-15 02:28:39

您可以使用模块级设置功能。根据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"

运行测试为您提供:

^{pr2}$

选择哪个名称并不重要,因此setup和{}的工作原理相同,但是{}更清晰。在

相关问题 更多 >

    热门问题