How to measure coverage when using multirpocessing via pytest?

前提是你 提交于 2020-04-16 05:09:13

问题


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 : (


回答1:


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/



来源:https://stackoverflow.com/questions/61143858/how-to-measure-coverage-when-using-multirpocessing-via-pytest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!