pythonpytestcoverage.pypytest-cov

Functional difference between `coverage run -m pytest` and `pytest --cov=...`?


The Coverage tool supports generating code coverage data from Pytest tests with coverage run -m pytest .... However, there is also the Pytest-Cov plugin, which invokes Coverage and generates coverage data by adding the --cov= option to Pytest.

However the Pytest-Cov documentation doesn't seem to explain anywhere how this differs from just using coverage run. Is there any practical difference, or is it just a matter of the options/configuration choices that are available?


Solution

  • The differences/advantages are mentioned on their Github README and docs

    Compared to just using coverage run this plugin does some extras:

    • Subprocess support: you can fork or run stuff in a subprocess and will get covered without any fuss.
    • Xdist support: you can use all of pytest-xdist's features and still get coverage.
    • Consistent pytest behavior. If you run coverage run -m pytest you will have slightly different sys.path (CWD will be in it, unlike when running pytest).

    All features offered by the coverage package should work, either through pytest-cov's command line options or through coverage's config file.