python - 如何在 pytest -k 命令行选项中包含正斜杠“/”
问题描述
我正在尝试使用 pytest -k 选项来过滤掉特定的测试。过滤器不起作用并在包含正斜杠“/”时返回错误。我尝试用一个和两个反斜杠转义它,但仍然是同样的错误。知道怎么做吗?
ERROR: Wrong expression passed to '-k':
test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml:
at column 22: unexpected character "/"
$ pytest tools/test_pinger.py --testinfo topoA_PEs.yml --params_file topoA_r28_params.yml --ulog -s --collect-only -k "test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml"
=============================================================================================================================== test session starts ===============================================================================================================================
platform linux -- Python 3.7.4, pytest-6.2.2, py-1.9.0, pluggy-0.13.0
rootdir: /home/as2863/pythonProjects/p1-automation, configfile: pytest.ini
plugins: csv-2.0.1, check-0.3.5, pylama-7.6.6, dependency-0.4.0, instafail-0.4.0, ordering-0.6, allure-pytest-2.8.20, repeat-0.7.0, reportportal-5.0.3
collected 18 items
<Package tools>
<Module test_pinger.py>
<Function test_pinger[pingtracer_topoA_L0.yml]>
<Function test_pinger[pingtracer_topoA_L10.yml]>
<Function test_pinger[pingtracer_topoA_ADI_L0.yml]>
<Function test_pinger[pingtracer_topoA_ADI_L10.yml]>
<Function test_pinger[pingtracer_topoA_AVPN_L0.yml]>
<Function test_pinger[pingtracer_topoA_AVPN_L10.yml]>
<Function test_pinger[pingtracer_topoA_MOW_L0.yml]>
<Function test_pinger[pingtracer_topoA_MOW_L10.yml]>
<Function test_pinger[new/pingtracer_topoA_US_PEs_L0.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_MOW_PLEAFS_L0_ipv4.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_MOW_PLEAFS_L0_ipv6.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_PEs_L0_ipv4.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_PEs_L0_ipv6.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_PEs_L10_ipv4.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml]>
<Function test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv6.yml]>
<Function test_pinger_mpls[pingtracer_topoA_ADI_L10.yml]>
<Function test_pinger_mpls[pingtracer_topoA_AVPN_L10.yml]>
=========================================================================================================================== 18 tests collected in 0.34s ===========================================================================================================================
ERROR: Wrong expression passed to '-k': test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml: at column 22: unexpected character "/"
(p1_netmiko_3-3-3) asilver@ubuntuP1-SYSlog-S1:~/pythonProjects/p1-automation$
解决方案
-k
是(有点故意)不灵活的——它并不意味着允许所有输入进行匹配。
也就是说,有两种方法可以做你想做的事:
- 稍微调整一下
-k
表情:pytest -k 'test_pinger and ping_topoA_ADI_US_PLEAFS_L0_ipv4'
- 直接使用测试ID (这似乎是你正在尝试的):
pytest 'tools/test_pinger.py::test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml]'
免责声明:我是 pytest 核心开发人员
推荐阅读
- c# - 如何将引导程序应用程序包含到 WIX msi 安装程序以检查 .NET 框架?
- deployment - Openshift 部署验证 - 质量检查
- c# - 比较几种注入配置的方式
- python - Python 从 selenium 启动 Chromedriver 在 IDLE 中有效,但在 Visual Studio Code 中无效
- php - 根据匹配的数组 ID 值创建新变量
- angular - Ngx 在角度通用中使用延迟加载进行翻译
- pandas - 为什么 Pandas df.to_hdf("a_file", "a_key") 多次执行时输出的大小会增加
- python - 从python中的excel文件导入pandas DataFrame时输入错误
- css - Angular 7 固定标题表示例不起作用
- bash - 在 bash 中添加和更改参数