Kevin Nguyen
Kevin Nguyen

Reputation: 13

how to create a new pytest command line flag that takes in an argument

how to create a new pytest command line flag that takes in an argument.

For example I have the following:

test_A.py::test_a
@pytest.mark.lvl1
def test_a():...
.
.
.
test_B.py::test_b
@pytest.mark.lvl10
def test_b():...

test_C.py::test_c
@pytest.mark.lvl20
def test_c():...

On command line i only want to run tests with marker levels less than equal to lvl10 (so lvl1 to lvl10 tests)

How can i do this without having to manually type on commandline pytest -m 'lvl1 or lvl2 or lvl3 ...'

I want to create a new command line pytest arg like: pytest --lte="lvl10"(lte is less than equal)

I was thinking somewhere along the lines where I want to define the --lte flag to do the following:

markers =[]

Do a pytest collect to find all tests that contain a marker that has 'lvl' in it and add that marker to the markers list only if the integer after 'lvl' is less than equal to 10 (lvl10). Then call a pytest -m on that list of markers ('lvl1 or lvl2 or lvl3 ...')

Upvotes: 1

Views: 3717

Answers (1)

Prem Anand
Prem Anand

Reputation: 2557

If you modify your marker to accept level as an argument, you can then run all tests less than or equal to the specified level, by adding a custom pytest_runtest_setup to your conftest.py

Sample Test

@pytest.mark.lvl(1)
def test_a():
   ...

conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption(
        "--level", type=int, action="store", metavar="num",
        help="only run tests matching the specified level or lower",
    )


def pytest_configure(config):
    # register the "lvl" marker
    config.addinivalue_line(
        "markers", "lvl(num): mark test to run only on named environment"
    )


def pytest_runtest_setup(item):
    test_level = next(item.iter_markers(name="lvl")).args[0]
    req_level  = item.config.getoption("--level")
    if test_level > req_level:
        pytest.skip(f"tests with level less than or equal to {req_level} was requested")

Sample invocation

pytest  --level 10

Upvotes: 3

Related Questions