0

How to preserve the database entries between tests in Django testing?

python3 manage.py test tests --keepdb

--keepdb preserves the database but not the table. The tables get flushed between the tests.

Here is pseudo-code

from django.test import TestCase

class test_1(TestCase):
    def function_1(self):
        # Creates an entry in the database with file_path
         someModel.objects.create(file_path=file_path)

class test_2(TestCase):
    def function_2(self):
        # needs the file_path to execute this function
         someModel.objects.get(file_path=file_path)

function_2 returns an error where file_path is not found since someModel table in the database has been flushed between the tests

How can I preserve the database table between the tests so they can find the file path?

This (talks about preserving the actual database and not the table) and this (creating a chain setUp does not work if I have 100´s tests that are chained) do not cover it.

PolarBear10
  • 2,065
  • 7
  • 24
  • 55
  • Why are those even 2 different tests since you need that data? There's no order guarantee between tests anyway, you might end up running `test_2` first and `test_1` second totally breaking it even if you use `SimpleTestCase` or `unittest.TestCase` to skip the transaction / rollbacks. Also unit tests as the name suggests is about testing individual units of code, not a sequence of actions (You could kind of simulate that by testing going from one state to another while setting up the initial state for each test) – Abdul Aziz Barkat Nov 07 '22 at 18:08
  • Even if I would to add `def function_2(self):` within the same class, it would still clear the data and get an error. How can we do a sequence of actions than as tests? – PolarBear10 Nov 07 '22 at 18:17
  • Don't the questions you linked already answer those? Just set up your data in functions like [setUpTestData](https://docs.djangoproject.com/en/4.1/topics/testing/tools/#django.test.TestCase.setUpTestData) or similar. – Abdul Aziz Barkat Nov 07 '22 at 18:20
  • @AbdulAzizBarkat, the challenge with `setUpTestData` is that it has to "re-run" to create the data needed instead of actually taking it from previous test cases. In my case, this is time-consuming since each function takes several minutes (machine learning model). So by the time the test has reached the 10th test case, it would have accumulated to hours. Therefore, I wanted to share the data table between the tests instead of recreating for each individual unit test. I hope this cleared it out. – PolarBear10 Nov 07 '22 at 18:24
  • I am going through PyTest now, and seems like they have `@pytest.fixture(scope="session")`, I will test it and see if it actually fits the purpose mentioned above. – PolarBear10 Nov 07 '22 at 18:26
  • What are you doing training your ML models in the tests? Perhaps you should look into mocking things... – Abdul Aziz Barkat Nov 07 '22 at 18:29
  • @AbdulAzizBarkat I am not training, I am testing the model output. – PolarBear10 Nov 07 '22 at 18:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249405/discussion-between-polarbear10-and-abdul-aziz-barkat). – PolarBear10 Nov 07 '22 at 18:38

0 Answers0