Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda