Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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__.:
like image 733
Sardathrion - against SE abuse Avatar asked Mar 10 '17 15:03

Sardathrion - against SE abuse


3 Answers

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
like image 55
feydaykyn Avatar answered Oct 17 '22 15:10

feydaykyn


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
like image 3
Anthony Avatar answered Oct 17 '22 15:10

Anthony


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.

like image 2
YPCrumble Avatar answered Oct 17 '22 15:10

YPCrumble