如何测试将记录作为参数的函数?
How can I test a function that takes record as a parameter?
我需要为文件 my_logging.py
中的函数 post_log_filter(record)
编写测试
#my_logging.py
import os
import sys
import traceback
log_format = '[%(levelname)s] %(asctime)s %(context)s %(pathname)s:%(lineno)d: %(message)s'
log_time_format = '%Y-%m-%dT%H:%M:%S%z'
def post_log_filter(record):
# filter out undesirable logs
logs_to_omit = [
{'filename': 'basehttp.py', 'funcName': 'log_message'}, # annoying logging any response to sys.stderr (even if status is 200)
{'filename': 'options.py', 'funcName': 'construct_change_message'}, # logging `Key not found` if a checkbox was unchecked and isn't present in POST data
]
if any([bool(record.__dict__.viewitems() >= r.viewitems()) for r in logs_to_omit]):
return False
return True
我写的测试是:
test_my_logging.py
from django.utils.unittest import TestCase
from my_logging import *
import collections
import mock
from mock import patch
import logging
LOGGER = logging.getLogger(__name__)
log_format = '[%(levelname)s] %(asctime)s %(context)s %(pathname)s:%(lineno)d: %(message)s'
log_time_format = '%Y-%m-%dT%H:%M:%S%z'
class TestMyLogging(TestCase):
def test_post_log_filter(self):
self.assertEqual(True, post_log_filter(logging.LogRecord(None, None, None, None, msg=None, args=None, exc_info=None, func=None)))
def test_post_log_filter_false(self):
record = logging.Formatter([
{'filename': 'basehttp.py', 'funcName': 'log_message'}, # annoying logging any response to sys.stderr (even if status is 200)
{'filename': 'options.py', 'funcName': 'construct_change_message'}, # logging `Key not found` if a checkbox was unchecked and isn't present in POST data
])
self.assertEqual(False, post_log_filter(record))
我正在测试两种情况。
对于真:
不管我为 post_log_filter()
传递什么,我都是真的。所以第一个测试通过了。
我不知道我在通过 record
测试 False
时做错了什么,我也为此得到了 True
。所以,测试失败。
你会如何推荐我通过记录以便我得到False
。
我不允许更改 my_logging.py
.
我找到了解决办法。
如下修改记录现在测试 False
.
def test_post_log_filter_false(self):
record = logging.makeLogRecord({'filename': 'basehttp.py', 'funcName': 'log_message'})
self.assertEqual(False, post_log_filter(record))
两个测试都通过了。
我需要为文件 my_logging.py
post_log_filter(record)
编写测试
#my_logging.py
import os
import sys
import traceback
log_format = '[%(levelname)s] %(asctime)s %(context)s %(pathname)s:%(lineno)d: %(message)s'
log_time_format = '%Y-%m-%dT%H:%M:%S%z'
def post_log_filter(record):
# filter out undesirable logs
logs_to_omit = [
{'filename': 'basehttp.py', 'funcName': 'log_message'}, # annoying logging any response to sys.stderr (even if status is 200)
{'filename': 'options.py', 'funcName': 'construct_change_message'}, # logging `Key not found` if a checkbox was unchecked and isn't present in POST data
]
if any([bool(record.__dict__.viewitems() >= r.viewitems()) for r in logs_to_omit]):
return False
return True
我写的测试是:
test_my_logging.py
from django.utils.unittest import TestCase
from my_logging import *
import collections
import mock
from mock import patch
import logging
LOGGER = logging.getLogger(__name__)
log_format = '[%(levelname)s] %(asctime)s %(context)s %(pathname)s:%(lineno)d: %(message)s'
log_time_format = '%Y-%m-%dT%H:%M:%S%z'
class TestMyLogging(TestCase):
def test_post_log_filter(self):
self.assertEqual(True, post_log_filter(logging.LogRecord(None, None, None, None, msg=None, args=None, exc_info=None, func=None)))
def test_post_log_filter_false(self):
record = logging.Formatter([
{'filename': 'basehttp.py', 'funcName': 'log_message'}, # annoying logging any response to sys.stderr (even if status is 200)
{'filename': 'options.py', 'funcName': 'construct_change_message'}, # logging `Key not found` if a checkbox was unchecked and isn't present in POST data
])
self.assertEqual(False, post_log_filter(record))
我正在测试两种情况。
对于真:
不管我为 post_log_filter()
传递什么,我都是真的。所以第一个测试通过了。
我不知道我在通过 record
测试 False
时做错了什么,我也为此得到了 True
。所以,测试失败。
你会如何推荐我通过记录以便我得到False
。
我不允许更改 my_logging.py
.
我找到了解决办法。
如下修改记录现在测试 False
.
def test_post_log_filter_false(self):
record = logging.makeLogRecord({'filename': 'basehttp.py', 'funcName': 'log_message'})
self.assertEqual(False, post_log_filter(record))
两个测试都通过了。