我有一个代码,我需要传递像终端名称这样的参数. 这是我的代码以及如何传递参数.我收到一个“文件未找到”的错误,我不明白. 我在终端中尝试了命令:pytest filename .py -almonds 我应该把
          这是我的代码以及如何传递参数.我收到一个“文件未找到”的错误,我不明白.
我在终端中尝试了命令: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
