Django test tables are not being created

后端 未结 4 1733
梦谈多话
梦谈多话 2021-01-13 15:33

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 gett

4条回答
  •  北海茫月
    2021-01-13 15:51

    pytest create table for unmanaged model during testing

    add --nomigrations to your pytest.ini or setup.cfg - wherever your pytest settings live:

    [pytest]
    addopts = --nomigrations
    

    and add this to your top-level 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
    

    It will do the magic

提交回复
热议问题