| 1 |
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" Tools for having doctest and unittest work together more nicely. |
| 12 |
|
| 13 |
Eclipse's PyDev plugin will run your unittest files for you very nicely. |
| 14 |
The doctest_for_module function allows you to easily run the doctest for a |
| 15 |
module along side your standard unit tests within Eclipse. |
| 16 |
""" |
| 17 |
|
| 18 |
# Standard library imports |
| 19 |
import doctest |
| 20 |
import unittest |
| 21 |
|
| 22 |
|
| 23 |
def doctest_for_module(module): |
| 24 |
""" Create a TestCase from a module's doctests that will be run by the |
| 25 |
standard unittest.main(). |
| 26 |
|
| 27 |
Example tests/test_foo.py:: |
| 28 |
|
| 29 |
import unittest |
| 30 |
|
| 31 |
import foo |
| 32 |
from traits.testing.api import doctest_for_module |
| 33 |
|
| 34 |
class FooTestCase(unittest.TestCase): |
| 35 |
... |
| 36 |
|
| 37 |
class FooDocTest(doctest_for_module(foo)): |
| 38 |
pass |
| 39 |
|
| 40 |
if __name__ == "__main__": |
| 41 |
# This will run and report both FooTestCase and the doctests in |
| 42 |
# module foo. |
| 43 |
unittest.main() |
| 44 |
|
| 45 |
Alternatively, you can say:: |
| 46 |
|
| 47 |
FooDocTest = doctest_for_module(foo) |
| 48 |
|
| 49 |
instead of:: |
| 50 |
|
| 51 |
class FooDocTest(doctest_for_module(foo)): |
| 52 |
pass |
| 53 |
""" |
| 54 |
|
| 55 |
class C(unittest.TestCase): |
| 56 |
def test_dummy(self): |
| 57 |
pass # Make the test case loader find us |
| 58 |
|
| 59 |
def run(self, result=None): |
| 60 |
# doctest doesn't like nose.result.TextTestResult objects, |
| 61 |
# so we try to determine if thats what we're dealing |
| 62 |
# with and use its internal result attribute instead |
| 63 |
if hasattr(result, "result"): |
| 64 |
doctest.DocTestSuite(module).run(result.result) |
| 65 |
else: |
| 66 |
doctest.DocTestSuite(module).run(result) |
| 67 |
|
| 68 |
return C |