【发布时间】:2013-05-18 14:01:59
【问题描述】:
我目前正在使用鼻子编写一些功能测试。我正在测试的库操纵目录结构。
为了获得可重现的结果,我存储了一个测试目录结构的模板,并在执行测试之前创建了一个副本(我在测试setup 函数中执行此操作)。这可以确保我在测试开始时始终具有明确定义的状态。
现在我还有两个要求:
- 如果测试失败,我希望不覆盖或删除它操作的目录结构,以便分析问题。
- 我希望能够并行运行多个测试。
这两个要求都可以通过为每个执行的测试创建一个具有不同名称的新副本来解决。出于这个原因,我想访问当前在 setup 函数中执行的测试的名称,以便我可以适当地命名副本。有什么方法可以实现吗?
一个说明性的代码示例:
def setup_func(test_name):
print "Setup of " + test_name
def teardown_func(test_name):
print "Teardown of " + test_name
@with_setup(setup_func, teardown_func)
def test_one():
pass
@with_setup(setup_func, teardown_func)
def test_two():
pass
预期输出:
Setup of test_one
Teardown of test_one
Setup of test_two
Teardown of test_two
将名称作为参数注入将是最好的解决方案,但我也愿意接受其他建议。
【问题讨论】:
标签: python unit-testing testing nose nosetests