Django test FileField using test fixtures

前端 未结 5 1342
囚心锁ツ
囚心锁ツ 2020-12-25 11:52

I\'m trying to build tests for some models that have a FileField. The model looks like this:

class SolutionFile(models.Model):
    \'\'\'
    A file from a s         


        
5条回答
  •  遥遥无期
    2020-12-25 12:28

    with pytest and pytest-django, I use this in conftest.py file:

    import tempfile
    import shutil
    from pytest_django.lazy_django import skip_if_no_django
    from pytest_django.fixtures import SettingsWrapper
    
    
    @pytest.fixture(scope='session')
    #@pytest.yield_fixture()
    def settings():
        """A Django settings object which restores changes after the testrun"""
        skip_if_no_django()
    
        wrapper = SettingsWrapper()
        yield wrapper
        wrapper.finalize()
    
    
    @pytest.fixture(autouse=True, scope='session')
    def media_root(settings):
        tmp_dir = tempfile.mkdtemp()
        settings.MEDIA_ROOT = tmp_dir
        yield settings.MEDIA_ROOT
        shutil.rmtree(tmp_dir)
    
    
    @pytest.fixture(scope='session')
    def django_db_setup(media_root, django_db_setup):
        print('inject_after')
    

    might be helpful:

    1. https://dev.funkwhale.audio/funkwhale/funkwhale/blob/de777764da0c0e9fe66d0bb76317679be964588b/api/tests/conftest.py
    2. https://framagit.org/ideascube/ideascube/blob/master/conftest.py
    3. https://stackoverflow.com/a/56177770/5305401

提交回复
热议问题