I can’t manage to have doctest test my modules classes methods.
My module looks like that:
JavaScript
x
4
1
simplemod
2
├── A.py
3
└── __init__.py
4
A.py
contains:
JavaScript
1
9
1
class A:
2
def method(self):
3
"""This is a test
4
5
>>> True
6
False
7
"""
8
pass
9
(so the tests should fail)
__init__.py
contains
JavaScript
1
4
1
from .A import A
2
3
__test__ = {'A': A}
4
And then I run
JavaScript
1
4
1
>>> import doctest, simplemod
2
>>> doctest.testmod(simplemod)
3
TestResults(failed=0, attempted=0)
4
Why doesn’t doctest test A.method
?
Advertisement
Answer
I found out why.
Why it doesn’t work
doctest tries to detect which tests don’t belong to the tested module, and doesn’t run them. This prevents running all the tests of your dependencies.
Here, my doctest belongs to simplemod.A
while I am testing simplemod
.
Recommended solution
From the doctest documentation about testing complex packages.
Rename A.py
to a.py
, and replace __init__.py
, with
JavaScript
1
8
1
from . import a
2
3
def load_tests(loader, tests, ignore):
4
import unittest
5
import doctest
6
tests.addTests(doctest.DocTestSuite(a))
7
return tests
8
You can then run your test with a simple
$ python -m unittest
in the parent folder.