Search code examples
djangocoverage.py

coverage in parallel for django tests


I am running the following through a Makefile:

NPROCS:=$(shell /usr/bin/nproc)

.PHONY: coverage-app
coverage-app:
    coverage erase --rcfile=./.coveragerc-app
    coverage run --parallel-mode --rcfile=./.coveragerc-app manage.py test -v 3 --parallel=$(NPROCS) app
    coverage combine --rcfile=./.coveragerc-app
    coverage report -m --rcfile=./.coveragerc-app

If I set NPROCS to 1, I get the expected 100% test coverage of all files within app. However, if NPROCS is greater than 1, I get lots of missing lines in my report.

What am I doing wrong?

My .coveragerc-app is as follows:

# Control coverage.py
[run]
branch = True
omit = */__init__*
       */test*.py
       */migrations/*
       */urls.py
       app/admin.py
       app/apps.py
source = app
parallel = true

[report]
precision = 1
show_missing = True
ignore_errors = True
exclude_lines =
    pragma: no cover
    raise NotImplementedError
    except ImportError
    def __repr__
    if self\.logger\.debug
    if __name__ == .__main__.:

Solution

  • You miss a few steps, from Measuring sub-processes:

    1. change the coverage run command to this one :
        COVERAGE_PROCESS_START=./.coveragerc-app coverage run 
        --parallel-mode --concurrency=multiprocessing 
        --rcfile=./.coveragerc-app manage.py test -v 3 --parallel=$(NPROCS) app
    
    1. Create a file named sitecustomize.py in your local folder with
        import coverage
        coverage.process_startup()
    
    1. Add concurrency option in your rcfile (run section):
        concurrency=multiprocessing