Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I integrate doctests with unittest's test discovery?

Tags:

I wrote a python script to do all my tests automatically for me, and generate a HTML report. I discovered discover for unittests the other day which lets me run all the unittests in a given directory without explicitly naming them, and I'd really like to be able to do my doctests the same way, rather than having to import each module explicitly.

I found some info on how to do this at https://docs.python.org/2/library/doctest.html but didn't really get it. Could you please help me with using discover with my doctests?

Python test discovery with doctests, coverage and parallelism is related, but still doesn't answer my question.

coverage_module

import coverage
import doctest
import unittest
import os

# import test_module 
import my_module

cov = coverage.Coverage()
cov.start()

# running doctest by explicity naming the module
doctest.testmod(my_module)

# running unittests by just specifying the folder to look into
testLoad = unittest.TestLoader()
testSuite = testLoad.discover(start_dir=os.getcwd())
runner = unittest.TextTestRunner()
runner.run(testSuite)

cov.stop()
cov.save()
cov.html_report()
print "tests completed"

test_module

import unittest
import doctest

from my_module import My_Class


class My_Class_Tests(unittest.TestCase):
    def setUp(self):
        # setup variables

    def test_1(self):
        # test code

# The bit that should load up the doctests? What's loader, tests, and ignore though? 
# Is this in the right place?
def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(module_with_doctests))
    return tests

if __name__ == '__main__':
    unittest.main()
like image 330
bluprince13 Avatar asked Feb 20 '17 09:02

bluprince13


People also ask

Can doctest be used to test docstrings?

The Doctest Module finds patterns in the docstring that looks like interactive shell commands. The input and expected output are included in the docstring, then the doctest module uses this docstring for testing the processed output.

Is PyUnit and unittest same?

PyUnit is an easy way to create unit testing programs and UnitTests with Python. (Note that docs.python.org uses the name "unittest", which is also the module name.)

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

The tests in the text file can be run from the command line, just as with the Python source modules. $ python -m doctest -v doctest_in_help.


1 Answers

Lets figure out what's happening there

1) unittest.discovery

It has no clue of doctests as doctests is a different framework. So unittest isn't supposed to discover doctests out of the box. That means you'll need to glue them together by hand

2) doctest

It's essentially a separate framework although it has some glueing classes to convert doctests into unittest-like TestCases. https://docs.python.org/2.7/library/doctest.html#doctest.DocTestSuite

3) discover

Didn't get what discover you mean, I suppose it's

python -m unittest discover

If not and you're talking about https://pypi.python.org/pypi/discover then just forget about it - it's a backport for earlier versions of python

4) what to do

either scatter a lot of load_tests hooks across your code as described here https://docs.python.org/2.7/library/doctest.html#unittest-api or code a method to collect all the modules your have in one place and convert them into a DocTestSuite[s] https://docs.python.org/2.7/library/doctest.html#doctest.DocTestSuite

But honestly neither approach makes any sense nowadays as it boils down to:

$ py.test --doctest-modules

or

$ nosetests --with-doctest

Of course coverage and lots of bells & whistles are also supplied by these frameworks and you may keep sticking to unittest.TestCase, and you won't even need to create a coverage_module so I would dig into one of them rather then trying to come up with your own solution

like image 181
ffeast Avatar answered Oct 11 '22 07:10

ffeast