好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在Python中如何通过命令行在pytest中传递参数

我有一个代码,我需要传递像终端名称这样的参数. 这是我的代码以及如何传递参数.我收到一个“文件未找到”的错误,我不明白.

我在终端中尝试了命令:pytest< filename> .py -almonds 我应该把这个名字打印成“杏仁”

@pytest.mark.parametrize("name")
def print_name(name):
    print ("Displaying name: %s" % name)
在你的pytest测试中,不要使用@ pytest.mark.parametrize:

def test_print_name(name):
    print ("Displaying name: %s" % name)

在conftest.py中:

def pytest_addoption(parser):
    parser.addoption("--name", action="store", default="default name")


def pytest_generate_tests(metafunc):
    # This is called for every test. Only get/set command line arguments
    # if the argument is specified in the list of test "fixturenames".
    option_value = metafunc.config.option.name
    if 'name' in metafunc.fixturenames and option_value is not None:
        metafunc.parametrize("name", [option_value])

然后,您可以使用命令行参数从命令行运行:

pytest -s tests/my_test_module.py --name abc

查看更多关于在Python中如何通过命令行在pytest中传递参数的详细内容...

  阅读:28次