I've moved recently from Python 2.7 to Python 3.8. There's a strange new phenomenon when running the tests, that can be reproduced with this simple example:
from django.test import TestCase
from users.models import User
class TestWTF(TestCase):
def setUp(self):
self.user = User.objects.create(email='admin@project.com')
def test_failure(self):
self.assertTrue(False)
def test_success(self):
pass
When test_failure()
fails the self.user
object doesn't get removed from the DB. It seems like the promised rollback feature just doesn't happen. test_success()
and all subsequent tests in the same class will fail with UNIQUE
constraint being violated when setUp()
tries to create the object again.
It doesn't happen in Python 2.
A partial output from pytest
I'm using:
$ pytest -W ignore -s
================================================= test session starts ==================================================
platform linux -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
django: settings: project_sites.settings.tests (from ini)
rootdir: /home/emes/devel/project/project, inifile: pytest.ini
plugins: django-3.8.0, env-0.6.2, case-1.5.3, cov-2.8.1
collected 2 items
[...]
=============================================== short test summary info ================================================
FAILED deals/tests/test_wtf.py::TestWTF::test_failure - AssertionError: False is not true
FAILED deals/tests/test_wtf.py::TestWTF::test_success - django.db.utils.IntegrityError: UNIQUE constraint failed: use...
================================================== 2 failed in 22.76s ==================================================
edit: I'm using Django-1.11.26