How to pass name of method in pytest.parametrize for executing them in test?

492 views Asked by At

I need to execute a specific fixture method, according to the id. I need smt like this.

@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'

What I see, it can be done next:

@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'
1

There are 1 answers

0
Павло Ракета On

Found the solution:

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

But need to change parametrized options to:

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