我有一个代码,我需要从终端传递参数,比如name。
下面是我的代码以及如何传递参数。我得到一个“文件未找到”类错误,我不明白
我已经在终端中尝试了命令:pytest<;文件名>;。py-杏仁
我应该把名字印成“杏仁”
@pytest.mark.parametrize(“名称”)
def打印名称(名称):
打印(“显示名称:%s”%name)
在pytest测试中,不要使用@pytest.mark.parametrize:
def测试打印名称(名称):
打印(“显示名称:%s”%name)
在conftest.py中:
def pytest\u addoption(解析器):
parser.addoption(“--name”,action=“store”,default=“default name”)
def pytest_generate_测试(metafunc):
#每次测试都需要这样做。仅获取/设置命令行参数
#如果参数在测试“fixturenames”列表中指定。
option_value=metafunc.config.option.name
如果metafunc.fixturenames和option_值中的“name”不是无:
metafunc.Parameterize(“名称,[option_值])
然后可以使用命令行参数从命令行运行:
pytest-s tests/my_test_module.py--名称abc