2

我最近遇到了一个关于在 Python 中组合单元测试和文档测试的问题。我以其他方式解决了这个问题,但我仍然对此有疑问。

Python 的 doctest 模块解析模块中的文档字符串,并在每一行的开头运行“>>>”之后的命令,并将其输出与文档字符串中的输出进行比较。

我想知道我是否可以在需要时使用 doctest 模块实现的比较方法。我知道可以将 doctest 作为测试用例添加到测试套件中,但在这里我想在单个测试用例中进行。

它是这样的:

class MyTest(TestCase):
    def testIt(self):
        # some codes like self.assertEqual(...)
        output = StringIO()
        with StdoutCollector(output):
            # do something that uses stdout
        # I want something like this:
        doctest.compare_result(output.getvalue(), 'expected output')
        # do more things

因为 doctest 使用一些启发式方法来比较省略号等输出。

有人会给出想法或建议吗?

4

1 回答 1

2

doctest.OutputChecker.check_output()

于 2009-06-13T13:00:27.557 回答