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.")
这种方法使命令行参数成为可选的。但我想让它们成为强制性的。
解决方案
要使用 将命令行参数标记为强制参数addoption
,请将required=True
其作为关键字参数添加到方法调用中
推荐阅读
- javascript - 当我使用 setState 回调来 toggleDropdown 我的下拉菜单在选择项目后打开
- java - HttpServletRequest getParameterMap - 单独获取表单参数
- c++ - 弗兰克·卢纳 (Frank Luna) 对他的书“使用 DirectX12 进行 3D 游戏编程”的介绍中的盒子示例的问题
- python - 如何通过css选择器查找包含属性和文本的元素
- python - 使用 Azure CLI、Rest API 或 Python 在 Azure ADLS gen2 中复制文件
- python - 具有超过 3 列的 ColumnTransformer
- overlap - 如何控制kmz中多边形的重叠顺序
- azure - 如何在发布管道中将文件保存到 Azure 存储帐户?
- python - INFO:tensorflow:Error 报告给 Coordinator:
,发现 2 个根错误 - javascript - 填充输入字段时启用 p:commandLink