通过pytest

问题描述

当我在View类中的方法pytest执行时,日志记录语句在控制台上不产生任何输出。那是预期的行为还是我错过了什么?

这是视图:

import logging
from django.views import View
logger = logging.getLogger(__name__)

class FancyWebhook(View):
    def post(self,request,*args,**kwargs):
        logger.info("DUMMY logging")
        print("DUMMY printing")
        return HttpResponse(status=200)

configs/settings/test.py中的日志记录配置:

# ...
LOGGING = {
    "version": 1,"disable_existing_loggers": False,"handlers": {
        "console": {"class": "logging.StreamHandler"},},"loggers": {
        "django": {
            "handlers": ["console"],"level": os.getenv("DJANGO_LOG_LEVEL","INFO"),}

测试方法

import pytest
@pytest.mark.django_db
def test_fancy_webook_empty_payload_fails(client):
    print("calling client.post()...")
    response = client.post('/pricing/fancywebhook',{},content_type='application/json')
    assert response.status_code == 403

通过pytest --ds=config.settings.test运行测试仅将assert 301 == 403错误打印到控制台,但不会显示“ DUMMY日志记录”或“ DUMMY打印”语句。

我肯定想念明显的东西,不是吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)