pytest-django
pytest-django copied to clipboard
Tests using `live_server` flush all data of my database
In order to access existing database, I put these code into conftest.py according to https://pytest-django.readthedocs.io/en/latest/database.html?highlight=rollback#using-an-existing-external-database-for-tests
@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'db.example.com',
'NAME': 'external_db',
}
After running test functions like this one, all data of the database were flushed.
def test_xxx(live_server):
r = requests.get(live_server + reverse("xxx"))
# some assertions
Is there anyway to use live_server fixture without modifying the existing database?
The live_server fixture uses transactional_db automatically.
I think there was another issue in this regard lately, and it might make sense to have those separate - but then you would also not see changes during the test.
I'd just like to mention that the flushing seems to be a waste of time if the --reuse-db option is not used, because the database is destroyed anyway.
Over lots of tests this is a big overhead.
I was using "--reuse-db" so far and it worked nicely. However, I now added live_server tests, and ever since I get broken tests after running my live_server tests.
Once example is that the flush also wipes out all data migrations! Like for instance setting the right domain on the Site model. These are then reset back to "example.com" which leads to broken links. :|
Any idea how to deal with the interplay of flushing and data migrations? Can I exclude certain apps from being flushed when using live_server? Is there such a setting?
I'd be happy to hear solution ideas & thoughts.