Python unittest:如何将stdout消息临时重定向到缓冲区并测试其内容?

2024-09-28 22:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想捕获在测试期间临时发送到stdout(我们的stderr)的消息,并断言这些消息中是否出现了某些字符串模式:

import unittest
class SomeTest(unittest.TestCase):
    def test_stdout(self):
        output = ""
        function_that_writes_to_stdout()
        # How to capture stdout in output temporarily?
        self.assertIn("some message", output)

我发现了一个similar question,但是公认的答案建议捕获所有测试用例发送到stdout的消息。你知道吗

是的,我知道对发送到stdout的消息进行单元测试不是很明智。是的,我也知道最好将logging^{}结合使用。假设这两个选项在这个阶段都不可用。你知道吗


Tags: to字符串importself消息outputdefstderr
1条回答
网友
1楼 · 发布于 2024-09-28 22:22:32

解决方案1。以下方法对我有效:

import io
import unittest
from contextlib import redirect_stdout

class Test(unittest.TestCase):
    def test_stdout(self):
        buf = io.StringIO()
        with redirect_stdout(buf):
            print("foo!")
        self.assertIn("foo", buf.getvalue())

buf.getvalue()将包含整个输出,包括\n字符。你知道吗

解决方案2.为了模拟^{}的行为,可以通过如下方法assertStdout扩展unittest.TestCase。你知道吗

class StdoutRedirectionContext():
    class ListIO():
        def __init__(self):
            # Container for messages sent to stdout.
            self.output = []
        def write(self, s):
            # Filter empty strings or naked newline characters.
            if s in ("\n", ""): return
            self.output.append(s)

    def __enter__(self):
        self._buf = self.ListIO()
        self._ctx = redirect_stdout(self._buf)
        self._ctx.__enter__()
        return self._buf

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self._ctx.__exit__(exc_type, exc_value, exc_traceback)

class TestCase(unittest.TestCase):
    def assertStdout(self):
        return StdoutRedirectionContext()

这里,StdoutRedirectionContext充当上下文管理器,单个消息将被收集在output列表中。扩展的TestCase可以如下所示在stdout上断言消息:

class AnotherTest(TestCase):
    def test_stdout(self):
        with self.assertStdout() as cm:
            print("foo!")
            print("bar!")
        self.assertIn("foo!", cm.output)
        self.assertIn("baz!", cm.output)

以上结果产生以下输出:

======================================================================
FAIL: test_stdout (__main__.AnotherTest)
                                   
Traceback (most recent call last):
  File "executor_test.py", line 440, in test_stdout
    self.assertIn("baz!", cm.output)
AssertionError: 'baz!' not found in ['foo!', 'bar!']

相关问题 更多 >