Skip to content

Commit d64b6bc

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents a47173e + 5d6a7ef commit d64b6bc

26 files changed

+367
-195
lines changed

.travis.yml

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,64 @@
1-
sudo: false
21
language: python
3-
dist: trusty
2+
dist: xenial
43

54
jobs:
65
fast_finish: true
76
include:
87
- stage: baseline
98
python: 3.6
10-
env: TOXENV=py36-dj20-postgres-coverage
9+
env: TOXENV=py36-dj20-postgres-xdist-coverage
10+
services:
11+
- postgresql
1112
- python: 3.6
1213
env: TOXENV=py36-dj111-sqlite-coverage
1314
- python: 2.7
1415
env: TOXENV=py27-dj111-mysql_innodb-coverage
16+
services:
17+
- mysql
1518
- python: 3.6
1619
env: TOXENV=checkqa,docs
1720

1821
- stage: test
19-
# py37 is not available in trusty dist, and requires sudo=true with xenial.
2022
python: 3.7
2123
env: TOXENV=py37-dj21-sqlite-coverage
22-
dist: xenial
23-
sudo: true
24+
- python: 3.7
25+
env: TOXENV=py37-dj22-sqlite-coverage
26+
27+
# Explicitly test (older) pytest 4.1.
28+
- python: 3.7
29+
env: TOXENV=py37-dj21-sqlite-pytest41-coverage
2430

2531
- python: 3.6
2632
env: TOXENV=py36-djmaster-sqlite-coverage
2733

2834
- python: 3.5
2935
env: TOXENV=py35-dj110-postgres-coverage
36+
services:
37+
- postgresql
3038

3139
- python: 3.4
3240
env: TOXENV=py34-dj19-sqlite_file-coverage
3341

3442
- python: 2.7
3543
env: TOXENV=py27-dj111-mysql_myisam-coverage
44+
services:
45+
- mysql
3646
- python: 2.7
3747
env: TOXENV=py27-dj18-postgres-coverage
48+
services:
49+
- postgresql
3850

3951
# pypy/pypy3: not included with coverage reports (much slower then).
40-
- python: pypy
52+
- python: pypy2.7-6.0
4153
env: TOXENV=pypy-dj111-sqlite_file
42-
- python: pypy3
54+
- python: pypy3.5-6.0
4355
env: TOXENV=pypy3-dj110-sqlite
4456

4557
- stage: test_release
4658
python: 3.6
4759
env: TOXENV=py36-dj20-postgres
60+
services:
61+
- postgresql
4862

4963
- stage: release
5064
script: skip
@@ -75,7 +89,7 @@ stages:
7589
if: tag IS present
7690

7791
install:
78-
- pip install tox==3.3.0
92+
- pip install tox==3.7.0
7993

8094
script:
8195
- tox

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pytest-django allows you to test your Django project/applications with the
2828
<https://pytest-django.readthedocs.io/en/latest/contributing.html>`_
2929
* Version compatibility:
3030

31-
* Django: 1.8-1.11, 2.0-2.1,
31+
* Django: 1.8-1.11, 2.0-2.2,
3232
and latest master branch (compatible at the time of each release)
3333
* Python: CPython 2.7, 3.4-3.7 or PyPy 2, 3
3434
* pytest: >=3.6

docs/changelog.rst

+37
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@ Features
1010
* Added a new option `--django-debug` to set the DEBUG setting to True prior to
1111
running tests.
1212

13+
3.4.7 (2019-02-03)
14+
------------------
15+
16+
Bugfixes
17+
^^^^^^^^
18+
19+
* Fix disabling/handling of unittest methods with pytest 4.2+ (#700)
20+
21+
3.4.6 (2019-02-01)
22+
------------------
23+
24+
Bugfixes
25+
^^^^^^^^
26+
27+
* django_find_project: add cwd as fallback always (#690)
28+
29+
Misc
30+
^^^^
31+
32+
* Enable tests for Django 2.2 and add classifier (#693)
33+
* Disallow pytest 4.2.0 in ``install_requires`` (#697)
34+
35+
3.4.5 (2019-01-07)
36+
------------------
37+
38+
Bugfixes
39+
^^^^^^^^
40+
41+
* Use ``request.config`` instead of ``pytest.config`` (#677)
42+
* :fixture:`admin_user`: handle "email" username_field (#676)
43+
44+
Misc
45+
^^^^
46+
47+
* Minor doc fixes (#674)
48+
* tests: fix for pytest 4 (#675)
49+
1350
3.4.4 (2018-11-13)
1451
------------------
1552

docs/helpers.rst

+10-4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ Example
178178
Using the `admin_client` fixture will cause the test to automatically be marked for database use (no need to specify the
179179
``django_db`` mark).
180180

181+
.. fixture:: admin_user
182+
181183
``admin_user`` - an admin user (superuser)
182184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
183185

@@ -283,17 +285,20 @@ Example
283285
``django_assert_num_queries``
284286
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285287

286-
.. py:function:: django_assert_num_queries(connection=None, info=None)
288+
.. py:function:: django_assert_num_queries(num, connection=None, info=None)
287289
290+
:param num: expected number of queries
288291
:param connection: optional non-default DB connection
289292
:param str info: optional info message to display on failure
290293

291294
This fixture allows to check for an expected number of DB queries.
292295

296+
If the assertion failed, the executed queries can be shown by using
297+
the verbose command line option.
298+
293299
It wraps `django.test.utils.CaptureQueriesContext` and yields the wrapped
294300
CaptureQueriesContext instance.
295301

296-
297302
Example usage::
298303

299304
def test_queries(django_assert_num_queries):
@@ -310,8 +315,9 @@ Example usage::
310315
``django_assert_max_num_queries``
311316
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
312317

313-
.. py:function:: django_assert_num_queries(connection=None, info=None)
318+
.. py:function:: django_assert_max_num_queries(num, connection=None, info=None)
314319
320+
:param num: expected maximum number of queries
315321
:param connection: optional non-default DB connection
316322
:param str info: optional info message to display on failure
317323

@@ -322,7 +328,7 @@ It is a specialized version of :fixture:`django_assert_num_queries`.
322328
Example usage::
323329

324330
def test_max_queries(django_assert_max_num_queries):
325-
with django_assert_max_num_queries(3):
331+
with django_assert_max_num_queries(2):
326332
Item.objects.create('foo')
327333
Item.objects.create('bar')
328334

pytest_django/fixtures.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
from __future__ import with_statement
44

55
import os
6+
import warnings
7+
from contextlib import contextmanager
68
from functools import partial
79

810
import pytest
911

10-
from contextlib import contextmanager
11-
1212
from . import live_server_helper
13-
1413
from .django_compat import is_django_unittest
15-
1614
from .lazy_django import skip_if_no_django
1715

1816
__all__ = [
@@ -105,14 +103,14 @@ def django_db_setup(
105103

106104
with django_db_blocker.unblock():
107105
db_cfg = setup_databases(
108-
verbosity=pytest.config.option.verbose,
106+
verbosity=request.config.option.verbose,
109107
interactive=False,
110108
**setup_databases_args
111109
)
112110

113111
def teardown_database():
114112
with django_db_blocker.unblock():
115-
teardown_databases(db_cfg, verbosity=pytest.config.option.verbose)
113+
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
116114

117115
if not django_db_keepdb:
118116
request.addfinalizer(teardown_database)
@@ -254,15 +252,16 @@ def admin_user(db, django_user_model, django_username_field):
254252
"""
255253
UserModel = django_user_model
256254
username_field = django_username_field
255+
username = "[email protected]" if username_field == "email" else "admin"
257256

258257
try:
259-
user = UserModel._default_manager.get(**{username_field: "admin"})
258+
user = UserModel._default_manager.get(**{username_field: username})
260259
except UserModel.DoesNotExist:
261260
extra_fields = {}
262-
if username_field != "username":
261+
if username_field not in ("username", "email"):
263262
extra_fields[username_field] = "admin"
264263
user = UserModel._default_manager.create_superuser(
265-
"admin", "[email protected]", "password", **extra_fields
264+
username, "[email protected]", "password", **extra_fields
266265
)
267266
return user
268267

@@ -362,11 +361,10 @@ def live_server(request):
362361
if django.VERSION >= (1, 11):
363362
ports = addr.split(":")[1]
364363
if "-" in ports or "," in ports:
365-
request.config.warn(
366-
"D001",
364+
warnings.warn(
367365
"Specifying multiple live server ports is not supported "
368366
"in Django 1.11. This will be an error in a future "
369-
"pytest-django release.",
367+
"pytest-django release."
370368
)
371369

372370
if not addr:

pytest_django/plugin.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import types
1313

1414
import pytest
15+
from pkg_resources import parse_version
1516

1617
from .django_compat import is_django_unittest # noqa
1718
from .fixtures import django_assert_num_queries # noqa
@@ -49,6 +50,9 @@
4950

5051
PY2 = sys.version_info[0] == 2
5152

53+
# pytest 4.2 handles unittest setup/teardown itself via wrapping fixtures.
54+
_handle_unittest_methods = parse_version(pytest.__version__) < parse_version("4.2")
55+
5256

5357
# ############### pytest hooks ################
5458

@@ -185,8 +189,11 @@ def find_django_path(args):
185189
args = map(str, args)
186190
args = [arg_to_path(x) for x in args if not x.startswith("-")]
187191

192+
cwd = pathlib.Path.cwd()
188193
if not args:
189-
args = [pathlib.Path.cwd()]
194+
args.append(cwd)
195+
elif cwd not in args:
196+
args.append(cwd)
190197

191198
for arg in args:
192199
if is_django_project(arg):
@@ -416,9 +423,9 @@ def _restore_class_methods(cls):
416423

417424

418425
def pytest_runtest_setup(item):
419-
if django_settings_is_configured() and is_django_unittest(item):
420-
cls = item.cls
421-
_disable_class_methods(cls)
426+
if _handle_unittest_methods:
427+
if django_settings_is_configured() and is_django_unittest(item):
428+
_disable_class_methods(item.cls)
422429

423430

424431
@pytest.fixture(autouse=True, scope="session")
@@ -514,16 +521,19 @@ def _cleaning_debug(self):
514521

515522
cls.debug = _cleaning_debug
516523

517-
_restore_class_methods(cls)
518-
cls.setUpClass()
519-
_disable_class_methods(cls)
520-
521-
def teardown():
524+
if _handle_unittest_methods:
522525
_restore_class_methods(cls)
523-
cls.tearDownClass()
524-
django_db_blocker.restore()
526+
cls.setUpClass()
527+
_disable_class_methods(cls)
525528

526-
request.addfinalizer(teardown)
529+
def teardown():
530+
_restore_class_methods(cls)
531+
cls.tearDownClass()
532+
django_db_blocker.restore()
533+
534+
request.addfinalizer(teardown)
535+
else:
536+
request.addfinalizer(django_db_blocker.restore)
527537

528538

529539
@pytest.fixture(scope="function", autouse=True)

pytest_django_test/app/migrations/0001_initial.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ class Migration(migrations.Migration):
99

1010
initial = True
1111

12-
dependencies = [
13-
]
12+
dependencies = []
1413

1514
operations = [
1615
migrations.CreateModel(
17-
name='Item',
16+
name="Item",
1817
fields=[
19-
('id', models.AutoField(auto_created=True, primary_key=True,
20-
serialize=False, verbose_name='ID')),
21-
('name', models.CharField(max_length=100)),
18+
(
19+
"id",
20+
models.AutoField(
21+
auto_created=True,
22+
primary_key=True,
23+
serialize=False,
24+
verbose_name="ID",
25+
),
26+
),
27+
("name", models.CharField(max_length=100)),
2228
],
23-
),
29+
)
2430
]

pytest_django_test/app/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
def admin_required_view(request):
99
if request.user.is_staff:
10-
return HttpResponse(Template('You are an admin').render(Context()))
11-
return HttpResponse(Template('Access denied').render(Context()))
10+
return HttpResponse(Template("You are an admin").render(Context()))
11+
return HttpResponse(Template("Access denied").render(Context()))
1212

1313

1414
def item_count(request):
15-
return HttpResponse('Item count: %d' % Item.objects.count())
15+
return HttpResponse("Item count: %d" % Item.objects.count())

pytest_django_test/compat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
try:
88
from django.conf.urls import patterns
99
except ImportError:
10+
1011
def patterns(prefix, *urls):
11-
assert prefix == ''
12+
assert prefix == ""
1213
return urls

0 commit comments

Comments
 (0)