Upcoming SlideShare
×

# using python module: doctest

3,019 views

Published on

How to use the python module of doctest.

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,019
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
12
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. &gt;&gt;&gt; multiply(3, 4) 12 &gt;&gt;&gt; multiply(&apos;a&apos;, 5) &apos;aaaaa&apos; “”” 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. &gt;&gt;&gt; gene_list(A()) [&lt;unpredictable.A object at 0x9c2214c&gt;] “”” return [obj]
5. 5. Handling Unpredictable Output # unpredictable2.py class A(object): pass def gene_list(obj): “”” Put obj into a list. &gt;&gt;&gt; gene_list(A()) #doctest: +ELLIPSIS [&lt;unpredictable2.A object at 0x...&gt;] “”” return [obj]
6. 6. Working Around Whitespace # double_space.py def double_space(lines): &amp;quot;&amp;quot;&amp;quot;Prints a list of lines double-spaced. &gt;&gt;&gt; double_space([&apos;Line one.&apos;, &apos;Line two.&apos;]) Line one. Line two. &amp;quot;&amp;quot;&amp;quot; for l in lines: print l print return
7. 7. Working Around Whitespace # double_space2.py def double_space(lines): &amp;quot;&amp;quot;&amp;quot;Prints a list of lines double-spaced. &gt;&gt;&gt; double_space([&apos;Line one.&apos;, &apos;Line two.&apos;]) Line one. &lt;BLANKLINE&gt; Line two. &lt;BLANKLINE&gt; &amp;quot;&amp;quot;&amp;quot; for l in lines: print l print return
8. 8. Test Locations # test_location.py “”” &gt;&gt;&gt; A == B False “”” class A(object): pass class B(object): pass
9. 9. Test Locations # test_location2.py from b_cls import B __test__ = { &apos;test001&apos;: “”” &gt;&gt;&gt; A.__class__ &lt;type &apos;type&apos;&gt; “”” , &apos;external&apos;: B, } class A(object): pass # b_cls.py “”” &gt;&gt;&gt; 1 + 1 3 “”” class B(object): “”” The class B. &gt;&gt;&gt; B.__name__ B “”” pass
10. 10. External Documentation # foo2.py def multiply(a, b): return a * b =============== How to use foo2.py =============== For numbers: &gt;&gt;&gt; from foo2 import multiply &gt;&gt;&gt; multiply(3, 7) 21 It also works with strings: &gt;&gt;&gt; multiply(&apos;s&apos;, 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(&apos; foo2.rst &apos;) 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(&apos;doctest_in_help.rst&apos;)) 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): &amp;quot;&amp;quot;&amp;quot; &gt;&gt;&gt; var = &apos;value&apos; &gt;&gt;&gt; &apos;var&apos; in globals() True &amp;quot;&amp;quot;&amp;quot; def two(self): &amp;quot;&amp;quot;&amp;quot; &gt;&gt;&gt; &apos;var&apos; in globals() False &amp;quot;&amp;quot;&amp;quot; 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 = &apos;value&apos; 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): &amp;quot;&amp;quot;&amp;quot; &gt;&gt;&gt; TestGlobals().one() &gt;&gt;&gt; &apos;var&apos; in _module_data True &amp;quot;&amp;quot;&amp;quot; _module_data[&apos;var&apos;] = &apos;value&apos; def two(self): &amp;quot;&amp;quot;&amp;quot; &gt;&gt;&gt; &apos;var&apos; in _module_data False &amp;quot;&amp;quot;&amp;quot;
19. 19. Test Context \$ python -m doctest -v doctest_mutable_globals.py ... Failed example: &apos;var&apos; in _module_data Expected: False Got: True ... 3 tests in 4 items. 2 passed and 1 failed. ***Test Failed*** 1 failures.
20. 20. Thanks!