3

I need to run only once parametrized test in pytest, for example I've got a dynamic list with test data and want to run test with test_data[0] parameters in case user send some condition for this, like mark

test_data = list()
test_data = create_test_data() #dynamic list depends on user conditions

@pytest.mark.parametrize("params", test_data)
def test_b(params):
    ...

1 Answers1

0

It is best to refactor you test to a config file and test. Then the simplest way would be to define several config files, in different folders, with different amount of data.

Alternatively, you an pass a parameter(s) and parse it, as below

tests:

def test_b(params):
    ...

content of conftest.py :

data = [....]


def pytest_addoption(parser):
    parser.addoption("--one", action="store_true", help="run one test")


def pytest_generate_tests(metafunc):
    if "params" in metafunc.fixturenames:
        if metafunc.config.getoption("one"):

            metafunc.parametrize("params", data[:1])
        else:
            metafunc.parametrize("params", data) 

See also How to pass arguments in pytest by command line

Serge
  • 3,387
  • 3
  • 16
  • 34