Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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()
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!