views:

64

answers:

1

I recently faced a problem about combining unit tests and doctests in Python. I worked around this problem in other way, but I still have question about it.

Python's doctest module parses docstrings in a module and run commands following ">>> " at the beginning of each line and compare the output of it and those in docstrings.

I wonder that I could use that comparison method implemented by doctest module when I want. I know that it's possible add doctest to test suite as a test case, but here I want to do it inside a single test case.

It is something like this:

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

Because doctest uses some heuristics to compare the outputs like ellipsis.

Would somebody give an idea or suggestions?

+1  A: 

See doctest.OutputChecker.check_output()

J.F. Sebastian
Thanks, that is exactly what I was looking for.
Achimnol