我正在尝试为我的 django 项目编写测试用例,但是当我运行时
“$ ./manage.py 测试”命令
它正在创建测试数据库,但没有创建任何表,并且我收到一个错误,表明表不存在。欢迎任何建议。这是我通过“./manage.pyspectdb>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 在测试期间为非托管模型创建表
add ——提名给你的pytest.ini
or setup.cfg
- 无论你的 pytest 设置在哪里:
[pytest]
addopts = --nomigrations
并将其添加到您的顶级conftest.py
@pytest.fixture(autouse=True, scope="session")
def django_test_environment(django_test_environment):
from django.apps import apps
get_models = apps.get_models
for m in [m for m in get_models() if not m._meta.managed]:
m._meta.managed = True
它会发挥魔法
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)