Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest:如何定义必需的命令行参数_Python_Pytest - Fatal编程技术网

Python pytest:如何定义必需的命令行参数

Python pytest:如何定义必需的命令行参数,python,pytest,Python,Pytest,如何定义自定义命令行参数,并在pytest中强制使用它们? 例如,我想要这样的东西: pytest-s sample.py——test_suite='some_value' 命令行参数--test_suite必须是强制性的。如果它丢失了,我想抛出一条类似“帮助”的消息,上面写着“进入要执行的测试套件” 这是我的代码: @pytest.fixture def testsuite(request): test_suite = request.config.getoption('--test_suit

如何定义自定义命令行参数,并在pytest中强制使用它们? 例如,我想要这样的东西: pytest-s sample.py——test_suite='some_value' 命令行参数--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.")   

这种方法使命令行参数成为可选的。但是我想使它们成为必需的。

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

required=True
添加到测试套件选项中是否有效?是!是的!非常感谢:)@MikeGillett您能添加您的评论作为答案,让Namratha接受吗?