0
votes

I have tests in pytest, each test has a unique marker that correlates to a test case number in TestRail, our test management system

Example:

@pytest.mark.C1234
def test_mytestC1234():
    pass

@pytest.mark.C1235
def test_mytestC1235():
    pass

I know I can run the tests from the command line:

pytest -m C1234 OR C1235

Is there a way to run the list of the markers from a file. The file will be built on the fly based on a test plan found in TestRail

The issue is that this list can get VERY large and a command line can not support that many characters.

Something like:

pytest -mf myfile.txt

Where the file myfile.txt contains a list of the markers.

1

1 Answers

0
votes

I couldn't find a built-in way to do this, but you can add a conftest.py file with the following (based partly on this hook).

RUNTESTS = "--run-tests"


def pytest_addoption(parser):
    group = parser.getgroup("filter")
    group.addoption(
        RUNTESTS,
        action="store",
        help="Path to file containing markers to run",
    )


def pytest_collection_modifyitems(config, items):
    if not config.getoption(RUNTESTS):
        return
    with open(config.getoption(RUNTESTS)) as f:
        markers = set(f.read().strip().split())
    deselected = []
    remaining = []
    for item in items:
        if any([mark.name in markers for mark in item.own_markers]):
            remaining.append(item)
        else:
            deselected.append(item)
    if deselected:
        config.hook.pytest_deselected(items=deselected)
        items[:] = remaining

Then, assuming you have a newline-separated list of markers in myfile.txt, you can run only the tests with those markers with:

pytest --run-tests myfile.txt