Python – How to include forward slash “/” in pytest -k command-line options

How to include forward slash “/” in pytest -k command-line options… here is a solution to the problem.

How to include forward slash “/” in pytest -k command-line options

I’m trying to use the pytest -k option to filter out specific tests. When the filter contains a forward slash “/”, the filter does not work and returns an error. I tried escaping it with one and two backslashes and still got the same error. Any idea how to do this?

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$

Solution

-k(somewhat deliberately) inflexible – it doesn’t mean that all inputs are allowed to match.

That said, there are two ways to do what you want:

  1. Adjust your -k expression slightly:pytest -k 'test_pinger and ping_topoA_ADI_US_PLEAFS_L0_ipv4'
  2. Use the test ID directly (which seems to be what you’re trying):pytest 'tools/test_pinger.py::test_pinger[pingtrace/ADI/ping_topoA_ADI_US_PLEAFS_L0_ipv4.yml] '

Disclaimer: I am a pytest core developer

Related Problems and Solutions