我已经配置了一个 Django 应用程序,调用lucy_web
它使用pybrake
. 在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
它是空的。
为什么我没有收到任何 Airbrake 测试通知?是不是更通用,比如在测试期间所有日志记录都被禁用/捕获?