Search code examples
pythonunit-testingpytestcoverage.pypytest-cov

How to measure coverage when using multirpocessing via pytest?


I run my unit tests via pytest. For coverage I use coverage.py.

In one of my unit tests, I run a function via multirpocessing and the coverage does not reflect the functions running via multirpocessing, but the asserts work. That's the problem I am trying to solve.

The test looks like so:

import time
import multiprocessing

def test_a_while_loop():
    # Start through multiprocessing in order to have a timeout.
    p = multiprocessing.Process(
        target=foo
        name="Foo",
    )
    try:
        p.start()
        # my timeout
        time.sleep(10)
        p.terminate()
    finally:
        # Cleanup.
        p.join()

    # Asserts below
    ...

To run the tests and see the coverage I use the following command in Ubuntu:

coverage run --concurrency=multiprocessing -m pytest my_project/
coverage combine
coverage report

In docs give guidance on what to do in order for coverage to account for multiprocessing correctly (here). So I have set up a .coveragerc like so:

[run]
concurrency = multiprocessing

[report]
show_missing = true

and also sitecustomize.py looks like so:

import coverage
coverage.process_startup()

Despite this, the above function running through multiprocessing is still not accounted for in coverage.

What am I doing wrong or missing?

P.S. This seems like a similar question, however it does not fix my problem again : (


Solution

  • I "fixed" this issue by doing two this:

    1. Switching the coverage package from coverage.py to pytest-cov.
    2. Adding this code above the process as described via their docs.

    Code:

    try:
        from pytest_cov.embed import cleanup_on_sigterm
    except ImportError:
        pass
    else:
        cleanup_on_sigterm()
    

    Then I simply run pytest --cov=my_proj my_proj/