Skip to content
Advertisement

Django Unit Test – IDs of created objects

Sample models.py

models.py

JavaScript

Suppose that I have written unit test/s:

JavaScript

I was wondering if it’s safe to assume that the id’s of the created objects in setUp() method will always start at id 1 and so on and so forth? If not, is there a specific reason why if the test database is always destroyed after running all tests?

Advertisement

Answer

It is not safe to assume that the ids will always start at 1 and increment. They may have higher ids if other tests have run beforehand and created Food rows, and unit tests are not executed in any guaranteed order.

Save references to the model instances on your test setup:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement