2

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 Answer 1

2

See doctest.OutputChecker.check_output()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.