22

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()
Community
  • 1
  • 1
bluprince13
  • 4,607
  • 12
  • 44
  • 91
  • 5
    `pytest` is considered etalon for tests in Python nowadays, so I'd recommend checking it out. Looks like it has a nice support for doctests too: http://doc.pytest.org/en/latest/doctest.html – yedpodtrzitko Feb 22 '17 at 23:57
  • 1
    Here's how I have done it in pytest. See this: https://medium.com/@vladyslav.krylasov/discover-unit-tests-and-doctests-in-one-run-c5504aea86bd – Vladyslav Krylasov Jan 15 '20 at 12:30

1 Answers1

9

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

Oleg Kuralenko
  • 11,003
  • 1
  • 30
  • 40
  • 2
    I'm so sad no solution is proposed using standard library, first by principle, but most practically because I'm working in a normative constrained environment where, good or ill, I'm not allowed to installed third-party libraries (even pytest!). – Joël Feb 22 '22 at 10:34