我已经配置了一个名为lucy_web
的Django应用程序,使用^{lucy_web
层次结构中的一个模块lucy_web.lib.session_recommendations
中,我定义了一个测试函数:
import logging
logger = logging.getLogger(__name__)
def log_something():
logger.error("Logging something...")
如果我从Django shell调用此函数,如下所示:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py shell
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from lucy_web.lib.session_recommendation import *
In [2]: log_something()
我看到一封错误电子邮件出现:
但是,如果我定义了一个测试,并尝试从此处调用它,则不会出现任何新的错误实例:
from django.test import TestCase
from django.core import mail
class SessionRecommendationTestCase(TestCase):
def test_airbrake_notification_if_session_type_does_not_exist(self):
from lucy_web.lib.session_recommendation import log_something
log_something()
import ipdb; ipdb.set_trace()
运行此操作不会导致Airbrake仪表板中出现任何新的错误实例。此外,mail.outbox
为空:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py test lucy_web.tests.test_session_recommendation.SessionRecommendationTestCase.test_airbrake_notification_if_session_type_does_not_exist
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
--Return--
None
> /Users/kurtpeek/Documents/Dev/lucy2/lucy-web/lucy_web/tests/test_session_recommendation.py(377)test_airbrake_notification_if_session_type_does_not_exist()
375 from lucy_web.lib.session_recommendation import log_something
376 log_something()
--> 377 import ipdb; ipdb.set_trace()
ipdb> mail.outbox
[]
我从https://docs.djangoproject.com/en/2.0/topics/testing/tools/#email-services了解到,在测试中,Django将Django发送的所有电子邮件重定向到一个虚拟发件箱mail.outbox
。不过,我不希望Airbrake出现这种情况,因为电子邮件可能是由它们的后端发送的——如果是这样,我也不希望mail.outbox
是空的。你知道吗
为什么我没有收到任何关于测试的空气制动通知?是否有更一般的情况,比如在测试期间禁用/捕获所有日志记录?你知道吗
我通过在
pybrake.Notifier
的send_notice
方法中设置一个跟踪来验证它不会在单元测试中被调用,而当我从Django shell“手动”调用函数时会被调用。你知道吗为了仍然获得测试覆盖率,我解决了这个问题,只是断言
logger.error
被调用:这是我要测试的实际函数:
此测试通过:
相关问题 更多 >
编程相关推荐