对于某些方法,我需要验证日志消息作为断言的一部分。
问题是,如果我仅运行该文件,它将正常工作。但是,当我运行所有测试用例时,它失败了,因为它找不到任何错误日志消息。
delete_later.py
class DeleteLater:
def message(self):
logging.info("info")
test_delete_later.py
class TestDeleteLater(unittest.TestCase):
def test_message(self):
with self.assertLogs(level='INFO') as cm:
DeleteLater().message()
assert "INFO:root:info" in cm.output
运行单个测试用例时通过
(.venv) ➜ ci-cd-framework git:(feature/DAC-962) ✗ py.test -v tests/test_delete_later.py
============================================================================================================ test session starts =============================================================================================================
platform darwin -- Python 3.4.9, pytest-3.2.3, py-1.7.0, pluggy-0.4.0 -- /demo/.venv/bin/python3
cachedir: .cache
rootdir: /demo, inifile:
collected 1 item
tests/test_delete_later.py::TestDeleteLater::test_message PASSED
运行所有测试失败
self = <recipes.tests.test_delete_later.TestDeleteLater testMethod=test_message>
def test_message(self):
with self.assertLogs(level='INFO') as cm:
DeleteLater().message()
> assert "INFO:root:info" in cm.output
E AssertionError: assert 'INFO:root:info' in []
E + where [] = _LoggingWatcher(records=[], output=[]).output
recipes/tests/test_delete_later.py:15: AssertionError
------------------------------------------------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------------------------------------------------
2019-01-13 16:47:12,270 - <MagicMock name='mock()' id='4594187624'> - info