小编Nam*_*tha的帖子

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.")   
Run Code Online (Sandbox Code Playgroud)

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

python pytest

5
推荐指数
1
解决办法
803
查看次数

标签 统计

pytest ×1

python ×1