简体   繁体   English

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

[英]pytest: How to define mandatory command line arguments

How can I define custom command line arguments, and make them mandatory in pytest?如何定义自定义命令行参数,并在 pytest 中强制使用它们? For example, I want something like this: pytest -s sample.py --test_suite='some_value' And the command line argument "--test_suite" has to be mandatory.例如,我想要这样的东西: pytest -s sample.py --test_suite='some_value' 命令行参数“--test_suite”必须是强制性的。 If its missing, I want to throw a "help" like message which says "enter the test_suite to be executed"如果它丢失,我想抛出一个“帮助”之类的消息,上面写着“输入要执行的 test_suite”

This is my code:这是我的代码:

@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.")   

This approach makes the command line arguments optional.这种方法使命令行参数成为可选的。 But I want to make them mandatory.但我想让它们成为强制性的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM