doctest Testing through documentation Honggang Wang @ Aragon Nov 2010
Getting Started # foo.py def multiply(a, b): “”” This is a demo function. >>> multiply(3, 4) 12 >>> mult...
Handling Unpredictable Output Sometimes the exact output may not  be predictable.  e.g. Local date and time values Object ...
Handling Unpredictable Output # unpredictable.py class A(object): pass def gene_list(obj): “”” Put obj into a list. >&g...
Handling Unpredictable Output # unpredictable2.py class A(object): pass def gene_list(obj): “”” Put obj into a list. >&...
Working Around Whitespace # double_space.py def double_space(lines): """Prints a list of lines ...
Working Around Whitespace # double_space2.py def double_space(lines): """Prints a list of lines...
Test Locations # test_location.py “”” >>> A == B False  “”” class A(object): pass class B(object): pass
Test Locations # test_location2.py from b_cls import B __test__ = { 'test001': “”” >>> A.__class__ <...
External Documentation # foo2.py def multiply(a, b): return a * b =============== How to use foo2.py =============== For n...
External Documentation $ python -m doctest -v foo2.rst Run test using foo2.rst
Running Tests # utils.py def add(a, b): return a + b if __name__ == “__main__”: import doctest doctest.testmod() By module...
Running Tests # other_script.py import doctest if __name__ == “__main__”: doctest.testfile(' foo2.rst ') By file
Unittest Suite # doctest_unittest.py import doctest import unittest import doctest_simple suite = unittest.TestSuite() sui...
Unittest Suite $ python doctest_unittest.py my_function (doctest_simple) Doctest: doctest_simple.my_function ... ok doctes...
Test Context class TestGlobals(object): def one(self): """ >>> var = 'value'...
Test Context $ python -m doctest -v doctest_test_globals.py Trying: var = 'value' Expecting nothing ok ... 2 ite...
Test Context _module_data = {} class TestGlobals(object): def one(self): """ >>> TestG...
Test Context $ python -m doctest -v doctest_mutable_globals.py ... Failed example: 'var' in _module_data Expecte...
Thanks!
Upcoming SlideShare
Loading in...5
×

using python module: doctest

1,305

Published on

How to use the python module of doctest.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,305
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

using python module: doctest

  1. 1. doctest Testing through documentation Honggang Wang @ Aragon Nov 2010
  2. 2. Getting Started # foo.py def multiply(a, b): “”” This is a demo function. >>> multiply(3, 4) 12 >>> multiply('a', 5) 'aaaaa' “”” return a * b $ python foo.py $ python -v foo.py $ python -m doctest -v foo.py Trying: multiply(3, 4) Expecting: 12 ok ... 2 passed and 0 failed. Test passed. Run test
  3. 3. Handling Unpredictable Output Sometimes the exact output may not be predictable. e.g. Local date and time values Object ids Precision of float values Exceptions And more...
  4. 4. Handling Unpredictable Output # unpredictable.py class A(object): pass def gene_list(obj): “”” Put obj into a list. >>> gene_list(A()) [<unpredictable.A object at 0x9c2214c>] “”” return [obj]
  5. 5. Handling Unpredictable Output # unpredictable2.py class A(object): pass def gene_list(obj): “”” Put obj into a list. >>> gene_list(A()) #doctest: +ELLIPSIS [<unpredictable2.A object at 0x...>] “”” return [obj]
  6. 6. Working Around Whitespace # double_space.py def double_space(lines): """Prints a list of lines double-spaced. >>> double_space(['Line one.', 'Line two.']) Line one. Line two. """ for l in lines: print l print return
  7. 7. Working Around Whitespace # double_space2.py def double_space(lines): """Prints a list of lines double-spaced. >>> double_space(['Line one.', 'Line two.']) Line one. <BLANKLINE> Line two. <BLANKLINE> """ for l in lines: print l print return
  8. 8. Test Locations # test_location.py “”” >>> A == B False “”” class A(object): pass class B(object): pass
  9. 9. Test Locations # test_location2.py from b_cls import B __test__ = { 'test001': “”” >>> A.__class__ <type 'type'> “”” , 'external': B, } class A(object): pass # b_cls.py “”” >>> 1 + 1 3 “”” class B(object): “”” The class B. >>> B.__name__ B “”” pass
  10. 10. External Documentation # foo2.py def multiply(a, b): return a * b =============== How to use foo2.py =============== For numbers: >>> from foo2 import multiply >>> multiply(3, 7) 21 It also works with strings: >>> multiply('s', 7) sssssss foo2.rst
  11. 11. External Documentation $ python -m doctest -v foo2.rst Run test using foo2.rst
  12. 12. Running Tests # utils.py def add(a, b): return a + b if __name__ == “__main__”: import doctest doctest.testmod() By module # other_script.py import utils if __name__ == “__main__”: import doctest doctest.testmod(utils)
  13. 13. Running Tests # other_script.py import doctest if __name__ == “__main__”: doctest.testfile(' foo2.rst ') By file
  14. 14. Unittest Suite # doctest_unittest.py import doctest import unittest import doctest_simple suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(doctest_simple)) suite.addTest(doctest.DocFileSuite('doctest_in_help.rst')) runner = unittest.TextTestRunner(verbosity=2) runner.run(suite)
  15. 15. Unittest Suite $ python doctest_unittest.py my_function (doctest_simple) Doctest: doctest_simple.my_function ... ok doctest_in_help.rst Doctest: doctest_in_help.rst ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.006s OK
  16. 16. Test Context class TestGlobals(object): def one(self): """ >>> var = 'value' >>> 'var' in globals() True """ def two(self): """ >>> 'var' in globals() False """ Each test source (function, class, module) has its own set of global values.
  17. 17. Test Context $ python -m doctest -v doctest_test_globals.py Trying: var = 'value' Expecting nothing ok ... 2 items passed all tests: 2 tests in doctest_test_globals.TestGlobals.one 1 tests in doctest_test_globals.TestGlobals.two 3 tests in 4 items. 3 passed and 0 failed. Test passed.
  18. 18. Test Context _module_data = {} class TestGlobals(object): def one(self): """ >>> TestGlobals().one() >>> 'var' in _module_data True """ _module_data['var'] = 'value' def two(self): """ >>> 'var' in _module_data False """
  19. 19. Test Context $ python -m doctest -v doctest_mutable_globals.py ... Failed example: 'var' in _module_data Expected: False Got: True ... 3 tests in 4 items. 2 passed and 1 failed. ***Test Failed*** 1 failures.
  20. 20. Thanks!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×