I want to create Model in unitest only for test. e.g. in file tests/test_helpers.py
import unittest
from unittest.mock import patch, Mock, MagicMock
from django.db import models
class YearNum(models.Model):
num = models.CharField(max_length=30)
class Meta:
managed = False
ordering = ['num']
def __str__(self):
return self.num
class TestExtraAttribute(unittest.TestCase):
pass # the test continues
but I got
RuntimeError: Model class strategic-cli.tests.test_helpers.YearNum doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
What I want to achieve is to create a simple model YearNum for unitest and to test for this purpose.
I thought it would help to create a DiscoverRunner I went according to the instructions (specifically this runner), however, it didn’t help and still leaves RuntimeError even though I loaded all the models on m._meta.managed = True.
Can anyone help me how to create a model just for testing purposes and not get the code unnecessary in the django application? Thanks
My only addition to what has already been said here is that the setup_test_app approach allows for the models to be picked up during test discovery and thus have their respective tables created while isolate_apps is more suited for interactions with models that don’t need to be backed by a table.