[Fixed]-Coverage in parallel for django tests

26👍

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

8👍

Took a read through the coverage documentation linked in answer above, perhaps it’s been updated, as just doing the following seems to work for our Django project:

coverage run --concurrency=multiprocessing manage.py test app --parallel=3
coverage combine
coverage report

3👍

Per the documentation:

Coverage should be run in a single process to obtain accurate statistics.

Not an answer to this question but for those looking to optimize their project in this manner it may be helpful to know it isn’t recommended by Django maintainers.

0👍

I have something like this:

function cpu_count() {
  python -c 'import multiprocessing; print(multiprocessing.cpu_count())'
}

function apps() {
  python -c "from website import apps; print(' '.join(apps.LOCAL_APPS))"
}

export $(cat envs/.env.development | xargs)

rm -rf htmlcov &&\
 coverage run --concurrency=multiprocessing\
  manage.py test --parallel $(cpu_count) $(apps) $@ &&\
 coverage combine &&\
 coverage html

Leave a comment