Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

[PyConTW 2013] doctest


Published on

Published in: Technology
  • Be the first to comment

[PyConTW 2013] doctest

  1. 1. doctestJenny JS Liang (jsliang)PyConTW 2013
  2. 2. It all began with MoSQL…(thank youMosky!)●● 2013年的春節,我在GitHub上看到Mosky開始著手進行MoSQL……● 3月的Taipei.py聚會,我問 Mosky:「為什麼妳的MoSQL文件可以寫得這麼快?」● Mosky:「因為我用doctest +Sphinx!」
  3. 3. A simple sample…# add.pydef add(a, b):"""This is an addition function.>>> add(1,2)3>>> add("Hello", "World")HelloWorld"""return a+b$ python -m doctest -v add.pyTrying:add(1,2)Expecting:3ok…1 items passed all tests:2 tests in add.add2 tests in 2 items.2 passed and 0 failed.Test passed.
  4. 4. Multilines# multilines.pydef multilines(input_str):""">>> input_str = 1st... 2nd... 3rd>>> multilines(input_str)1st2nd3rd"""print input_str$ python -m doctest -v multilines.pyTrying:input_str = 1st2nd3rdExpecting nothingokTrying:prnt_multilines(input_str)...Test passed.
  5. 5. Handling blank lines…# blank.pydef blankline(line1, line2):"""Print "line1nnline2".>>> blankline("Hello", "World")Hello<BLANKLINE>World"""print line1print ""print line2$ python -m doctest -v blank.pyTrying:blankline("Hello", "World")Expecting:Hello<BLANKLINE>Worldok...Test passed.
  6. 6. Checking Examples in DocstringsEnd your module with the following code:if __name__ == "__main__":import doctestdoctest.testmod()and execute:$ python <module_name>.py
  7. 7. Autodocumentingwith Sphinx
  8. 8. Module Structure● mymodule/○○○○ multilines.pyMake sure mymodule is accessible:$ export PYTHONPATH=$PYTHONPATH:/path/to/mymodule
  9. 9. Modify index.rst (use ReST)Welcome to mymodules documentation!====================================.. toctree:::maxdepth: 2.. automodule:: mymodule.add:members:.. automodule:: mymodule.blank:members:
  10. 10. Generating documentation withSphinx1. $ pip install sphinx2. $ sphinx-quickstart3. Follow the instructions. When you seePlease indicate if you want to use one of thefollowing Sphinx extensions:> autodoc: automatically insertdocstrings from modules (y/N) [n]: yselect "y"!4. modify index.rst (see the previous slide)5. $ make html
  11. 11. More information of Sphinx...● Sphinx● reStructuredText● sphinx.ext.autodoc – Include documentationfrom docstrings○ .. automodule::○ .. autoclass::○ .. autoexception::○ ...
  12. 12. References● Python doctest○● Documenting Your Project Using Sphinx○● sphinx.ext.autodoc – Include documentationfrom docstrings○● Minimal Sphinx setup for autodocumentingPython modules○