Tutorial :Using doctest “result parser” within unit-tests in Python?



Question:

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?


Solution:1

See doctest.OutputChecker.check_output()


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »