This article is the 23rd day article of Python Part 2 Advent Calendar 2015.
To measure unit test coverage with Django, it's standard to use django-nose.
If you search a little for how to set it, a lot of information will come out, so I will not repost it here. As for the articles on Qiita, the following articles are still valid even now that Django 1.9 has been released.
Try setting this minimal Django application to use django-nose
for coverage. I will measure it.
$ ./manage.py test sample --with-coverage --cover-package=sample --cover-html
nosetests sample --with-coverage --cover-package=sample --cover-html --verbosity=1
Creating test database for alias 'default'...
.
Name Stmts Miss Cover Missing
--------------------------------------------------
sample.py 0 0 100%
sample/settings.py 21 9 57% 58, 76, 112-130
sample/urls.py 4 0 100%
sample/views.py 5 0 100%
--------------------------------------------------
TOTAL 30 9 70%
----------------------------------------------------------------------
Ran 1 test in 0.011s
OK
Destroying test database for alias 'default'...
In most cases you should be fine with django-nose, but at the moment the latest version of django-nose == 1.4.2
[has not caught up with the new features in Django 1.8](https: // I'm curious about github.com/django-nose/django-nose/issues/244).
Also, it's becoming harder to feel the benefits of django-nose, except that it's easier to output coverage, so I'm becoming more motivated to use Django's standard test runner.
According to Post this stackoverflow, [Coverage.py](https://pypi.python.org/ It seems that you can also measure coverage directly using pypi / coverage), so I tried it myself.
In the sample Django application I mentioned earlier, this is the revision (https://github.com/hunza/django-sample-app/tree/48949c2b018d371c19067214b413da61f2ab52ce).
Run tests while measuring coverage.
$ coverage run --source=sample --omit='*/tests/*' manage.py test sample
Output coverage report.
$ coverage report
Name Stmts Miss Cover
----------------------------------------
sample/__init__.py 0 0 100%
sample/settings.py 20 0 100%
sample/urls.py 4 0 100%
sample/views.py 5 0 100%
sample/wsgi.py 4 4 0%
----------------------------------------
TOTAL 33 4 88%
Output coverage report in HTML to cover
directory.
$ coverage html --directory=cover
The coverage measurement feature of django-nose just wraps Coverage.py, so the output is almost the same for both console and HTML reports.
If you define .coveragerc
in the working directory, you can omit the options of the above command.
[run]
omit = */tests/*
[html]
directory = cover
Execution example.
$ coverage run --source=sample manage.py test sample
$ coverage report
$ coverage html
See documentation for .coveragerc
.
I've shown an example of measuring coverage with Coverage.py instead of django-nose.
There is no doubt that it is convenient because django-nose can do things that can not be realized without executing the command three times without django-nose, but if you automate it well with CI, Coverage.py alone Doing it is also an option.
Recommended Posts