pytestã§coverageã測ããããããããã°ãè²ã ã¹ãã¼ãã¢ããããäºæãããã
ã¨ããããããã£ã¦ã¿ã話ã
- venvç°å¢ãä½ã
- pytestã§ãã¹ãã§ããããã«ãã
- pytest-covã使ã
- ã«ãã¬ãã¸ä¸ãã¦ã¿ã
- djangoã§pytestã§ã«ãã¬ãã¸ãå³ã
- djangoã®æºå
- pytestã§djangoããã¹ã
- pytest + django + coverage
- çµãã
venvç°å¢ãä½ã
$ python3.6 -m venv coverage $ . coverage/bin/activate (coverage) $ python -V Python 3.6.5
pytestã§ãã¹ãã§ããããã«ãã
ã¨ãããããpytestã§ãã¹ãã§ããããã«ãã¨ãã
(coverage) $ pip install pytest (coverage) $ pip freeze | grep pytest pytest==3.5.1
test_my_module.py
import my_module def test_case_1(): source = 1 result = my_module.function1(source) assert str(source) == result
my_module.py
def function1(x): return str(x)
(coverage) $ pytest test_my_module.py ===================================== test session starts ===================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 rootdir: /Users/mtb/work/coverage/work, inifile: collected 1 item test_my_module.py . [100%] ================================== 1 passed in 0.00 seconds ===================================
OK
pytest-covã使ã
ã¾ããã¤ã³ã¹ãã¼ã«ããã
(coverage) $ pip install pytest-cov (coverage) $ pip freeze | grep pytest-cov pytest-cov==2.5.1
ããã¦å®è¡ããã
(coverage) $ py.test --cov my_module =============================================================== test session starts =============================================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 rootdir: /Users/mtb/work/coverage/work, inifile: plugins: cov-2.5.1 collected 1 item test_my_module.py . [100%] ---------- coverage: platform darwin, python 3.6.5-final-0 ----------- Name Stmts Miss Cover ---------------------------------- my_module.py 2 0 100% ============================================================ 1 passed in 0.02 seconds =============================================================
便å©ã ã
ã«ãã¬ãã¸ä¸ãã¦ã¿ã
ããã¦ãã«ãã¬ãã¸ãä¸ãããããªãã¨ããã¦ã¿ãã
def function1(x): return str(x) def function2(x): if x == 1: return True return False
(coverage) $ py.test --cov my_module =============================================================== test session starts =============================================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 rootdir: /Users/mtb/work/coverage/work, inifile: plugins: cov-2.5.1 collected 1 item test_my_module.py . [100%] ---------- coverage: platform darwin, python 3.6.5-final-0 ----------- Name Stmts Miss Cover ---------------------------------- my_module.py 6 3 50% ============================================================ 1 passed in 0.02 seconds =============================================================
OK. ã«ãã¬ãã¸ä¸ãã£ãã
ãã¹ãã追å ããã
import my_module def test_case_1(): source = 1 result = my_module.function1(source) assert str(source) == result def test_case_2(): source = 1 result = my_module.function2(source) assert result == True def test_case_3(): source = 2 result = my_module.function2(source) assert result == False
(coverage) $ py.test --cov my_module =============================================================== test session starts =============================================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 rootdir: /Users/mtb/work/coverage/work, inifile: plugins: cov-2.5.1 collected 3 items test_my_module.py ... [100%] ---------- coverage: platform darwin, python 3.6.5-final-0 ----------- Name Stmts Miss Cover ---------------------------------- my_module.py 6 0 100% ============================================================ 3 passed in 0.03 seconds =============================================================
100%ã«ãªã£ãã
djangoã§pytestã§ã«ãã¬ãã¸ãå³ã
djangoã§ãã«ãã¬ãã¸æ¸¬ããããã¨ããããã§æ¸¬ã£ã¦ã¿ãã
djangoã®æºå
django åå¿è ã ãã1.11ã«ãã¨ãã
(coverage) $ pip install django==1.11
(coverage) $ django-admin startproject myproject (coverage) $ cd myproject/ (coverage) $ ls manage.py myproject
ããã¦ãéãªã¬ã¹ãã³ã¹ãè¿ãããã«ä¿®æ£ã
myproject/url.py
from django.conf.urls import url from django.contrib import admin from django.http.response import HttpResponse def index(request): return HttpResponse('Hello Djano') urlpatterns = [ url(r'^', index), url(r'^admin/', admin.site.urls), ]
éã«åä½ç¢ºèªãã¦åããã¨ã確èªãd
(coverage) $ python manage.py runserver
$ curl http://localhost:8000 Hello Djano
pytestã§djangoããã¹ã
pytest-djangoãã¤ã³ã¹ãã¼ã«ããããã¼ã¸ã§ã³ã¯ç¾æç¹ã®ææ°çã
(coverage) $ pip install pytest-django (coverage) $ pip freeze | grep pytest-django pytest-django==3.2.1
pytest.iniã¨ããè¨å®ãã¡ã¤ã«ã追å
[pytest] DJANGO_SETTINGS_MODULE = myproject.settings python_files = tests.py test_*.py *_tests.py
ãã¹ãã追å ã
myproject/tests.py
from django.test import TestCase, Client class IndexTest(TestCase): def test_http(self): client = Client() response = client.get('/') self.assertEqual(response.status_code, 200)
ãã¹ããå®è¡ã
(coverage) $ pytest =============================================================== test session starts =============================================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 Django settings: myproject.settings (from ini file) rootdir: /Users/mtb/work/coverage/work/myproject, inifile: pytest.ini plugins: django-3.2.1, cov-2.5.1 collected 1 item myproject/tests.py . [100%] ============================================================ 1 passed in 0.20 seconds =============================================================
ãã¹ãã§ããã
pytest + django + coverage
ã¨ããããæå®ãããã§ããã
(coverage) $ pytest --cov myproject =============================================================== test session starts =============================================================== platform darwin -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0 Django settings: myproject.settings (from ini file) rootdir: /Users/mtb/work/coverage/work/myproject, inifile: pytest.ini plugins: django-3.2.1, cov-2.5.1 collected 1 item myproject/tests.py . [100%] ---------- coverage: platform darwin, python 3.6.5-final-0 ----------- Name Stmts Miss Cover ------------------------------------------- myproject/__init__.py 0 0 100% myproject/settings.py 18 0 100% myproject/tests.py 6 0 100% myproject/urls.py 6 0 100% myproject/wsgi.py 4 4 0% ------------------------------------------- TOTAL 34 4 88% ============================================================ 1 passed in 0.27 seconds =============================================================
ãµãã測ãã¦ããã
ã¡ãªã¿ã«ãcoverageãå³ãããã±ã¼ã¸ã¯æå®ããªãã¨ãå ¨ç¯å²ã対象ã«ãªã£ã¦ãæéãããããã大éã«åºã¦ããã
çµãã
ã¨ãããããdjangoãpytest使ã£ã¦coverage測ããããã«ãã£ãã
å®éã®ä»äºã§ãã©ãã©ã使ã£ã¦è¡ããã