Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 7ccf106

Browse files
authored
[django] Setup pytest-django (#995)
* [django] Setup pytest-django * fix djangorestframework tests
1 parent 6e4d64c commit 7ccf106

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

tests/contrib/django/conftest.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import django
3+
from django.conf import settings
4+
5+
# We manually designate which settings we will be using in an environment variable
6+
# This is similar to what occurs in the `manage.py`
7+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.contrib.django.app.settings')
8+
9+
10+
# `pytest` automatically calls this function once when tests are run.
11+
def pytest_configure():
12+
settings.DEBUG = False
13+
if django.VERSION < (1, 7, 0):
14+
settings.configure()
15+
else:
16+
django.setup()

tests/contrib/djangorestframework/app/settings.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
USE_I18N = True
2222
USE_L10N = True
2323
STATIC_URL = '/static/'
24-
ROOT_URLCONF = 'app.views'
24+
ROOT_URLCONF = 'tests.contrib.djangorestframework.app.views'
2525

2626
TEMPLATES = [
2727
{
@@ -111,5 +111,5 @@
111111
'rest_framework.permissions.IsAdminUser',
112112
],
113113

114-
'EXCEPTION_HANDLER': 'app.exceptions.custom_exception_handler'
114+
'EXCEPTION_HANDLER': 'tests.contrib.djangorestframework.app.exceptions.custom_exception_handler'
115115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import django
3+
from django.conf import settings
4+
5+
# We manually designate which settings we will be using in an environment variable
6+
# This is similar to what occurs in the `manage.py`
7+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.contrib.djangorestframework.app.settings')
8+
9+
10+
# `pytest` automatically calls this function once when tests are run.
11+
def pytest_configure():
12+
settings.DEBUG = False
13+
if django.VERSION < (1, 7, 0):
14+
settings.configure()
15+
else:
16+
django.setup()

tests/contrib/djangorestframework/runtests.py

-18
This file was deleted.

tests/contrib/djangorestframework/test_djangorestframework.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_unpatch(self):
3535
assert len(spans) == 1
3636
sp = spans[0]
3737
assert sp.name == 'django.request'
38-
assert sp.resource == 'app.views.UserViewSet'
38+
assert sp.resource == 'tests.contrib.djangorestframework.app.views.UserViewSet'
3939
assert sp.error == 0
4040
assert sp.span_type == 'http'
4141
assert sp.get_tag('http.status_code') == '500'
@@ -52,7 +52,7 @@ def test_trace_exceptions(self):
5252
assert len(spans) == 1
5353
sp = spans[0]
5454
assert sp.name == 'django.request'
55-
assert sp.resource == 'app.views.UserViewSet'
55+
assert sp.resource == 'tests.contrib.djangorestframework.app.views.UserViewSet'
5656
assert sp.error == 1
5757
assert sp.span_type == 'http'
5858
assert sp.get_tag('http.method') == 'GET'

tox.ini

+4-6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ deps =
138138
pytest>=3
139139
pytest-benchmark
140140
pytest-cov
141+
pytest-django
141142
opentracing
142143
psutil
143144
# test dependencies installed in all envs
@@ -378,9 +379,9 @@ commands =
378379
cassandra_contrib: pytest {posargs} tests/contrib/cassandra
379380
celery_contrib: pytest {posargs} tests/contrib/celery
380381
dbapi_contrib: pytest {posargs} tests/contrib/dbapi
381-
django_contrib: python tests/contrib/django/runtests.py {posargs}
382-
django_contrib_autopatch: python tests/ddtrace_run.py python tests/contrib/django/runtests.py {posargs}
383-
django_drf_contrib: python tests/contrib/djangorestframework/runtests.py {posargs}
382+
django_contrib: pytest {posargs} tests/contrib/django
383+
django_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/django
384+
django_drf_contrib: pytest {posargs} tests/contrib/djangorestframework
384385
elasticsearch_contrib: pytest {posargs} tests/contrib/elasticsearch
385386
falcon_contrib: pytest {posargs} tests/contrib/falcon/test_middleware.py tests/contrib/falcon/test_distributed_tracing.py
386387
falcon_contrib_autopatch: python tests/ddtrace_run.py pytest {posargs} tests/contrib/falcon/test_autopatch.py
@@ -423,9 +424,6 @@ commands =
423424
unit_tests: pytest {posargs} tests/unit
424425
benchmarks: pytest --benchmark-only {posargs} tests/benchmark.py
425426

426-
setenv =
427-
DJANGO_SETTINGS_MODULE = app.settings
428-
429427
[testenv:wait]
430428
commands=python tests/wait-for-services.py {posargs}
431429
basepython=python

0 commit comments

Comments
 (0)