Giordano
Giordano

Reputation: 5570

Providing initial data django unit test

I'm working on a Django application which have a lot of unit tests.
Some of those test functionalities on unmanaged models (Manage = False). In order to test them, there are in setUpClass the execution of some sql files.

Now, after upgrading at Django 1.11.2, some warnings are raised during the execution of sql files.
The warning is the following:

Warning: Unknown table '<table name>' return self.cursor.execute(query, args)

I searched the reason of this warning and seems it is linked to the sql file that check if the table exists before create or drop it.

I would remove this warnings, and to do this, I thought of drop the table in tearDownClass, in order to remove the IF EXISTS statements in sql files.
I think that this approach makes unit tests slower and I don't know if it is a best practice.

Furthermore, I found in Django documentation that is possible use fixtures to providing initial data. Since documentation is not very clear (from my point of view) on how use fixtures in unit tests, I'm asking this question to understand how is the best practice/approach to solve this problem.

Thanks in advance

Upvotes: 1

Views: 761

Answers (1)

Raj Subit
Raj Subit

Reputation: 1557

I did not understand your concern fully. However if the problem is loading initial data (fixtures) for testing, that can be done with following procedures:

class Test1(TestCase):
    fixtures = [
       "fixtures.json",
    ]

    def setUp(self):
        ...

    // test cases

More information on https://docs.djangoproject.com/en/1.11/topics/testing/tools/#fixture-loading

Update

Django 5.0 docs (Feb 2024)

Upvotes: 4

Related Questions