【发布时间】:2019-10-09 16:41:55
【问题描述】:
我想在两个不同的 pytest 运行中一个接一个地运行两个不同的测试,然后绑定一个清理功能以始终在第二次测试后运行。但是,只运行清理函数也应该具有灵活性。
我使用pytest缓存实现了第一次和第二次测试之间的交互。第一个测试做一些事情并将必要的数据写入 pytest 缓存。使用不同的 pytest 命令运行的第二个测试从该缓存中读取并执行其他操作。我通过在几个 pytest 钩子中设置属性和获取属性来使用测试类的自变量作为缓存。
在这种情况下,我将清理显示为测试,但我不想将其称为test_,因为它不是测试并且不应向用户显示结果。
# filename test_cases.py
class TestCase1:
@pytest.mark('one')
def test_one(self, fixture1):
# fixture1.y = 1
self.x = 1
assert self.x + fixture1.y == 2
@pytest.mark('two')
def test_two(self, fixture2):
# fixture2.y = 2
self.x += fixture2.y
assert self.x == 3
@pytest.mark('cleanup')
@pytest.mark('two')
@pytest.mark.run('after=test_two')
def test_cleanup(self, fixture):
# Let's say this cleans stuff
fixture.y = 0
这一系列命令将运行完整的测试。
pytest test_cases.py -m "one"
收集了 1 个测试... (test_one)
pytest test_cases.py -m "two"
收集了 2 个测试..(test_two 和清理)
在第二个命令中,它显示为两个测试,这就是整个问题所在。无论如何要从结果中排除清理但执行清理部分?
或者通过将test_cleanup 更改为cleanup 可以达到同样的效果吗?这里的问题是,对于不同的测试用例,包括它作为参数的固定装置,清理功能显然是不同的。
【问题讨论】:
标签: python-3.x pytest