Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using doctests from within unittests

Tags:

python

I typically write both unittests and doctests in my modules. I'd like to automatically run all of my doctests when running the test suite. I think this is possible, but I'm having a hard time with the syntax.

I have the test suite

import unittest
class ts(unittest.TestCase):
    def test_null(self): self.assertTrue(True)
if __name__ == '__main__': unittest.main()

I'd like to add to this suite all of the doctests in module module1. How can I do this? I've read the python docs, but I'm not any closer to success, here. Adding the lines

import doctest
import module1
suite = doctest.DocTestSuite(module1)

doesn't work. unittest.main() searches through the current file scope and runs every test case it finds, right? But DocTestSuite produces a test suite. How do I get unittest.main() to run the additional cases in the suite? Or am I just confused and deluded??

Once again, I'd be grateful for any help anyone can offer.

like image 377
Rick Avatar asked Apr 15 '11 19:04

Rick


People also ask

What is the correct way to run all Doctests in a given file from the command line?

To run the tests, use doctest as the main program via the -m option to the interpreter. Usually no output is produced while the tests are running, so the example below includes the -v option to make the output more verbose.

What indicates the beginning of a test case in Doctests what about the end?

Examples cannot usually stand on their own as explanations of a function, so doctest also allows for surrounding text. It looks for lines beginning with the interpreter prompt ( >>> ) to find the beginning of a test case, and the case is ended by a blank line or by the next interpreter prompt.

How do I run all Python Doctests?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.


2 Answers

I would recommend to use pytest --doctest-modules without any load_test protocol. You can simply add both the files or directories with your normal pytests and your modules with doctests to that pytest call.

pytest --doctest-modules path/to/pytest/unittests path/to/modules

It discovers and runs all doctests as well.

See https://docs.pytest.org/en/latest/doctest.html

like image 107
Alexander Pacha Avatar answered Oct 19 '22 06:10

Alexander Pacha


An update to this old question: since Python version 2.7 there is the load_tests protocol and there is no longer a need to write custom code. It allows you to add a function load_tests(), which a test loader will execute to update its collection of unit tests for the current module.

Put a function like this in your code module to package the module's own doctests into a test suite for unittest:

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite())
    return tests

Or, put a function like this into your unit test module to add the doctests from another module (for example, package.code_module) into the tests suite which is already there:

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(package.code_module))
    return tests

When unittest.TestLoader methods loadTestsFromModule(), loadTestsFromName() or discover() are used unittest uses a test suite including both unit tests and doctests.

like image 41
wodny Avatar answered Oct 19 '22 07:10

wodny