|
12 | 12 | import types |
13 | 13 |
|
14 | 14 | import pytest |
| 15 | +from pkg_resources import parse_version |
15 | 16 |
|
16 | 17 | from .django_compat import is_django_unittest # noqa |
17 | 18 | from .fixtures import django_assert_num_queries # noqa |
|
49 | 50 |
|
50 | 51 | PY2 = sys.version_info[0] == 2 |
51 | 52 |
|
| 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 | + |
52 | 56 |
|
53 | 57 | # ############### pytest hooks ################ |
54 | 58 |
|
@@ -416,9 +420,9 @@ def _restore_class_methods(cls): |
416 | 420 |
|
417 | 421 |
|
418 | 422 | 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) |
| 423 | + if _handle_unittest_methods: |
| 424 | + if django_settings_is_configured() and is_django_unittest(item): |
| 425 | + _disable_class_methods(item.cls) |
422 | 426 |
|
423 | 427 |
|
424 | 428 | @pytest.fixture(autouse=True, scope="session") |
@@ -508,16 +512,19 @@ def _cleaning_debug(self): |
508 | 512 |
|
509 | 513 | cls.debug = _cleaning_debug |
510 | 514 |
|
511 | | - _restore_class_methods(cls) |
512 | | - cls.setUpClass() |
513 | | - _disable_class_methods(cls) |
514 | | - |
515 | | - def teardown(): |
| 515 | + if _handle_unittest_methods: |
516 | 516 | _restore_class_methods(cls) |
517 | | - cls.tearDownClass() |
518 | | - django_db_blocker.restore() |
| 517 | + cls.setUpClass() |
| 518 | + _disable_class_methods(cls) |
519 | 519 |
|
520 | | - request.addfinalizer(teardown) |
| 520 | + def teardown(): |
| 521 | + _restore_class_methods(cls) |
| 522 | + cls.tearDownClass() |
| 523 | + django_db_blocker.restore() |
| 524 | + |
| 525 | + request.addfinalizer(teardown) |
| 526 | + else: |
| 527 | + request.addfinalizer(django_db_blocker.restore) |
521 | 528 |
|
522 | 529 |
|
523 | 530 | @pytest.fixture(scope="function", autouse=True) |
|
0 commit comments