How can I persist data to DB when using pytest/pytest-django in a test-run of a Django application?
I run pytest with py.test --nomigrations --reuse-db -s
and the Postgres DB test_<configured_db_name>
is created as expected, however nothing seems to be persisted to DB between tests and at the end of the test run the DB is empty.
import pytest
from django.contrib.auth.models import User
@pytest.mark.django_db(transaction=False)
def test_insert_user():
user = User.objects.create_user(username="test_user", email="[email protected]", password="test")
users = User.objects.all()
assert len(users) > 0
@pytest.mark.django_db(transaction=False)
def test_check_user():
users = User.objects.all()
assert len(users) > 0
The first test passes, the second does not making me wonder if anything is persisted to DB at all. According to the pytest-django documentation @pytest.mark.django_db(transaction=False)
will not rollback whatever has been affected by the decorated test.
Thank you,
/David