如何在pytest.parametrize中传递方法的名称以在测试中执行它们



我需要根据id执行一个特定的fixture方法。我需要这样的smt。

@pytest.fixture(autouse=True)
def create_instance_of_class():
test_instance_of_class = TestClass()
yield test_instance_of_class

@pytest.mark.paramethize('id, name_of_method', [
pytest.param('1', test_instace_of_class.do_smt_1()),
pytest.param('2', test_instace_of_class.do_smt_2()),
pytest.param('3', test_instace_of_class.do_smt_3()),
])
def test_some_test(id, name_of_method, test_instance_of_class)
assert name_of_method == test_instance_of_class.SUCCESSFUL_EXIT_CODE, 'Unsuccessful'

我所看到的,它可以做下一步:

@pytest.mark.paramethize('id', [
pytest.param('1'),
pytest.param('2'),
pytest.param('3'),
])
def test_some_test(id, test_instance_of_class)
if id == 1:
assert test_instace_of_class.do_smt_1() == test_instance_of_class.SUCCESSFUL_EXIT_CODE, 'Unsuccessful'
elif id == 2:
assert test_instace_of_class.do_smt_2() == test_instance_of_class.SUCCESSFUL_EXIT_CODE, 'Unsuccessful'

找到解决方案:

assert getattr(test_instance_of_class, name_of_method) == test_instance_of_class.SUCCESSFUL_EXIT_CODE

但需要将参数化选项更改为:

pytest.param('1', 'do_smt_1')

相关内容

  • 没有找到相关文章