According to the documentation, --reuse-db
should be used or addopts = --reuse-db
in pytest.ini
. I tried both and they don't work. The current tests have to signup and authenticate a new user at the start of each test to be able to access features requiring login. This makes tests run slowly and with the number of tests growing, this is becoming less convenient. Here's an example to demonstrate:
@pytest.mark.django_db
class TestSignIn:
def test_valid_user(self, client, valid_user_data, django_user_model, django_db_keepdb):
client_signup(client, valid_user_data, django_db_keepdb)
response = activate_user(client)
assert 'sign out' in get_text(response)
def test_sign_in(self, client, valid_user_data, django_user_model, django_db_keepdb):
client_signup(client, valid_user_data)
activate_user(client)
response = client.post(reverse('signout'), follow=True)
assert 'sign in' in get_text(response)
response = client_sign_in(client, valid_user_data)
assert 'sign out' in get_text(response)
In test_valid_user
, a new user is created and after user activation, a user shows up:
>>> django_user_model.objects.count()
1
and I verify using:
>>> django_db_keepdb
True
I want to reuse the user created in the previous test instead of creating a new one. With --reuse-db
specified, I'm expecting the second test test_sign_in
to detect the very same user. Instead, I get:
>>> django_user_model.objects.count()
0