using python module: doctest
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

using python module: doctest

  • 1,634 views
Uploaded on

How to use the python module of doctest.

How to use the python module of doctest.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,634
On Slideshare
1,634
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
12
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. doctest Testing through documentation Honggang Wang @ Aragon Nov 2010
  • 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. 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. 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. 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. 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. 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. Test Locations # test_location.py “”” >>> A == B False “”” class A(object): pass class B(object): pass
  • 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. 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. External Documentation $ python -m doctest -v foo2.rst Run test using foo2.rst
  • 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. Running Tests # other_script.py import doctest if __name__ == “__main__”: doctest.testfile(' foo2.rst ') By file
  • 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. 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. 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. 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. 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. 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. Thanks!