I am trying to automate testing across several modules. All of these modules have a "test()" function with their unit-tests. Some modules are basic and their tests contain simple statements, but most modules have unittest or doctest. I'm having the most trouble dynamically importing and running doctest.
For example, here is a module sample.py
class sample:
"""
>>> import sample
>>> print sample.hello()
Hello
"""
def hello():
return "Hello"
def test():
import doctest
doctest.testmod(name=__name__, verbose=True)
And here is my file run_all_tests.py
:
# assume I already have a list of all my files to test
for file in all_my_files:
temp_module = __import__(file)
temp_module.test()
This doesn't work and I always get this error:
1 items had no tests:
sample
0 tests in 1 items.
0 passed and 0 failed.
Test passed.
Please help me understand the problem.
Would Nose be a good alternative? I don't want to use it because I won't know beforehand if a module uses doctests, unittests or simple statements. But do let me know if that's not true/you have another alternative entirely!
Use doctest.DocTestSuite. It takes a module, extracts all
doctests
that exist there, and returns it as a unittest.TestSuite. Then, running the tests is a piece of pie. Your code would look like this:Why your code wasn't working
From doctest's
testmod
documentation:So, since you left out the first argument (
m
), the module__main__
is passed totestmod
. So the doctests that were run were the doctests in the module that contain thefor
loop. You can see this for yourself:run_tests.py
If you run your example now (before fixing it) you'll see that you'll get:
Clearly showing that the doctests running are those in
run_tests.py
. Thename
argument only changes the name that appears in the message (sample
).