[PYTHON] Measure Django application coverage with Coverage.py

This article is the 23rd day article of Python Part 2 Advent Calendar 2015.

Measure coverage with Django

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.

-Testing with Django

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'...

Measure coverage with Coverage.py

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.

Finally

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

Measure Django application coverage with Coverage.py
Web application creation with Django
Deploy a Django application with Docker
Twitter posting application made with Django
Build a web application with Django
Application development using SQLite with Django (PTVS)
I made a WEB application with Django
Internationalization with django
CRUD with Django
[Note] Measure coverage when running Django unit tests
Create a one-file hello world application with django
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
Web application made with Python3.4 + Django (Part.1 Environment construction)
File upload with django
Use LESS with Django
Single sign-on to your Django application with AWS SSO
Pooling mechanize with Django
A memo about building a Django (Python) application with Docker
Use MySQL with Django
Start today with Django
Getting Started with Django 2
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Get started with Django! ~ Tutorial ⑤ ~
Create an API with Django
Check python coverage with pytest-cov
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Web application development with Flask
Create a homepage with django
WebSocket application with Flask-Socket IO
Get started with Django! ~ Tutorial ④ ~
Measure WiFi speed with Python
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Do AES encryption with DJango
View flask coverage with pytest-cov
Getting Started with Python Django (6)
Rename an existing Django application
Combine two images with Django
Getting Started with Django with PyCharm
Web application with Python + Flask ② ③
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)