[英]How to run all pytest tests with extra function calls using class
我有一个名为 BaseTest 的基础 class,我所有的测试用例都继承自它。
每个 TestCase 都有 X 个测试和一个方法 - different_configuration()
而不是为每个测试创建 test_xyz 和 test_xyz_different(这意味着在代码中将我的测试加倍,如果我有 x 个不同的配置,甚至更多)
有没有办法让基础 class 有一些东西,甚至是一个夹具,每个现有测试将创建/运行另一个测试,但会在开始时调用 self.different_configuration() 。
额外信息 - 我有一个 dict 作为具有配置的属性,并且有一个配置,对于每个测试,我想测试两个可用选项。
self.different_configuration() 只是在测试本身之前更改配置值。
插图 -
def test_x(self):
self.run_and_verify()
def test_x_different_configuration(self):
self.different_configurations()
self.run_and_verify()
因此,对于每个现有测试,我在开始时使用此 function 调用创建另一个测试,是否有更清洁的方法来实现它?
您可以参数化一个夹具。
但是,您必须将配置值从字典中提取到列表中,才能在params
参数上使用它。
import pytest
@pytest.fixture(scope="module", params=[config1, config2])
def different_configuration(request):
return request.param
然后,在您的测试中,您可以使用每个配置值以某种方式根据需要更改配置:
def test_x(self, different_configuration):
self.different_configurations(different_configuration)
self.run_and_verify()
或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.