I'm trying to write test cases for my django project but when I run "$ ./manage.py test" command its creating test database but its not creating any tables and I'm getting an error that table does't exists. Any suggestions are welcome. Here is my model which i have created through "./manage.py inspectdb > models.py"
class MyCustomModel(models.Model):
name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
class Meta:
managed = False
db_table = 'MY_TABLE'
pytest create table for unmanaged model during testing
add --nomigrations to your
pytest.ini
[pytest] addopts = --nomigrations
and add this to your top-level
conftest.py
It will do the magic
You need to use:
This is replace by
From django 1.7
Do you have migrations? Run
python manage.py makemigrations
, the DB that's buildt during a test run uses them.Your table is unmanaged (
managed = False
) so it does not get created automatically during migration or testing.managed = False
lineIf 2, and you're using simple
manage.py test
, the best solution I've found is to add a test runner that modifies the managed flag on any unmanaged models. Mine looks like this in a runners.py file:It is activated by adding this line to settings.py:
Finally, I'm here today because we started using pytest for testing and the solution above stopped working. After a bunch of searching, I discovered a fix in the comments to one of the pages credited in the sample above (source, credit to Jan Murre)
We put the above code in conftest.py and our tests started working again.