2

我只需要在 pytest 中运行一次参数化测试,例如,我有一个包含测试数据的动态列表,并希望使用 test_data[0] 参数运行测试,以防用户为此发送一些条件,例如标记

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

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

4

1 回答 1

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

于 2019-09-05T18:31:43.350 回答