pytest:如何定义强制命令行参数

时间:2018-12-28 02:08:31

标签: python pytest

如何定义自定义命令行参数,并使其在pytest中成为必需参数? 例如,我想要这样的东西: pytest -s sample.py --test_suite ='some_value' 命令行参数“ --test_suite”必须是必需的。如果它不见了,我想抛出一条“帮助”之类的消息,上面写着“输入要执行的test_suite”

这是我的代码:

@pytest.fixture
def testsuite(request):
test_suite = request.config.getoption('--test_suite')
return test_suite

def pytest_addoption(parser):
parser.addoption("--test_suite",action="store",default="default_suite",
                 help="Enter the test suite you want to execute."
                      "Ex. --test_suite=default_suite"
                      "If nothing is selected, default_suite tests will be run.")   

此方法使命令行参数为可选。但是我想让它们成为强制性的。

1 个答案:

答案 0 :(得分:1)

要使用addoption将命令行参数标记为强制参数,请将required=True作为关键字参数添加到方法调用中

相关问题