2016-11-08 9 views
0

DEBUGTrueに設定されているかどうかを知る必要があるレシーバーがsettings.pyにあります。PytestとDjango設定のランタイムの変更

from django.conf import settings 
... 
@receiver(post_save, sender=User) 
def create_fake_firebaseUID(sender, instance, created=False, **kwargs): 
    # Fake firebaseUID if in DEBUG mode for development purposes 
    if created and settings.DEBUG: 
     try: 
      instance.userprofile 
     except ObjectDoesNotExist: 
      UserProfile.objects.create(user=instance, firebaseUID=str(uuid.uuid4())) 

問題は、私がmanage.py shellを使用してユーザーを作成すると、すべて正常に動作するということです。しかし、py.testでテストを実行すると、settings.DEBUGの値はFalseに変わります。 conftest.pypytest_configureにチェックした場合、DEBUGTrueに設定されています。それは後でどこかで変わり、私はどこが分からないのか。

これはどうしてですか?私は自分のコードでどこでも変更しないと確信しています。

編集。同様の問題を持っている人のため

conftest.py

import uuid 

import pytest 
import tempfile 
from django.conf import settings 
from django.contrib.auth.models import User 


@pytest.fixture(scope='session', autouse=True) 
def set_media_temp_folder(): 
    with tempfile.TemporaryDirectory() as temp_dir: 
     settings.MEDIA_ROOT = temp_dir 
     yield None 


def create_normal_user() -> User: 
    username = str(uuid.uuid4())[:30] 
    user = User.objects.create(username=username) 
    user.set_password('12345') 
    user.save() 
    return user 


@pytest.fixture 
def normal_user() -> User: 
    return create_normal_user() 


@pytest.fixture 
def normal_user2() -> User: 
    return create_normal_user() 

myappに/テスト/ conftest.py

# encoding: utf-8 
import os 

import pytest 
from django.core.files.uploadedfile import SimpleUploadedFile 

from userprofile.models import ProfilePicture 


@pytest.fixture 
def test_image() -> bytes: 
    DIR_PATH = os.path.dirname(os.path.realpath(__file__)) 
    with open(os.path.join(DIR_PATH, 'test_image.jpg'), 'rb') as f: 
     yield f 


@pytest.fixture 
def profile_picture(test_image, normal_user) -> ProfilePicture: 
    picture = SimpleUploadedFile(name='test_image.jpg', 
           content=test_image.read(), 
           content_type='image/png') 
    profile_picture = ProfilePicture.objects.get(userprofile__user=normal_user) 
    profile_picture.picture = picture 
    profile_picture.save() 
    return profile_picture 

pytest.ini

[pytest] 
addopts = --reuse-db 
DJANGO_SETTINGS_MODULE=mysite.settings 
+0

あなたのテストにコードを提供しないと、あなたのテストで 'DEBUG'が' False'と表示されている理由を言うのは難しいです。また、あなたの 'conftest.py'を見ておくと便利です。 –

+1

また、 'django.confからのインポート設定 'をインポートしていますか?設定モジュールを直接インポートすると、これは奇妙な問題を引き起こす可能性があります。 –

答えて

0

。私はその理由を見つけた。 pytest-djangoのソースファイルをダウンロードし、pytest-django/pytest_django/plugin.py:338にDEBUGをFalseに設定していることがわかりました。私はなぜthoを知らない。

関連する問題