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

using python module: doctest

on

  • 1,532 views

How to use the python module of doctest.

How to use the python module of doctest.

Statistics

Views

Total Views
1,532
Views on SlideShare
1,532
Embed Views
0

Actions

Likes
2
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    using python module: doctest using python module: doctest Presentation Transcript

    • 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 >>> 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
    • 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...
    • 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]
    • 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]
    • 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
    • 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
    • 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__ <type 'type'> “”” , 'external': B, } class A(object): pass # b_cls.py “”” >>> 1 + 1 3 “”” class B(object): “”” The class B. >>> B.__name__ B “”” pass
    • 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
    • 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 # other_script.py import utils if __name__ == “__main__”: import doctest doctest.testmod(utils)
    • 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() suite.addTest(doctest.DocTestSuite(doctest_simple)) suite.addTest(doctest.DocFileSuite('doctest_in_help.rst')) runner = unittest.TextTestRunner(verbosity=2) runner.run(suite)
    • 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
    • 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.
    • 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.
    • 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 """
    • 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.
    • Thanks!