How can I save data in 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 Postgres DB test_<configued_db_name> is created as expected, but nothing is stored in the DB between tests and at the end of the test run 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=" test_user@test.com ", 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 make me wonder if anything is stored in the database at all. According to the documentation, pystest-django @pytest.mark.django_db(transaction=False) will not roll back from what the decorated test influenced.
Thanks,
/ David
source share