Notes on how to use doctest

What is doctest

A python standard library for running simple tests

How to use

1. Write a test

Just write the execution content and the correct return value as a set in the docstring

def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(-8, -2)
    -10
    '''
    pass

if __name__ == '__main__':
    import doctest
    doctest.testmod()

2. Run the test

terminal


python hoge.py

3. Result

output


**********************************************************************
File "__main__", line 3, in __main__.add
Failed example:
    add(1, 2)
Expected:
    3
Got nothing
**********************************************************************
File "__main__", line 5, in __main__.add
Failed example:
    add(-8, -2)
Expected:
    -10
Got nothing
**********************************************************************
1 items had failures:
   2 of   2 in __main__.add
***Test Failed*** 2 failures.
TestResults(failed=2, attempted=2)

4. Correct

Correct it so that it returns correctly

hoge.py


def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(-8, -2)
    -10
    '''
    return a + b

if __name__ == '__main__':
    import doctest
    doctest.testmod()

5. Test again

terminal


python hoge.py

6. Result

No output if all tests are successful

output


Test by specifying a specific function

You can use doctest.testmod () to test all functions, but if you want to test only specific functions, use doctest.run_docstring_examples (). If you write as follows, only add () will be tested.

hoge.py


    import doctest
    doctest.run_docstring_examples(add, globals())

When using with jupyter notebook

It's the same to test all the functions defined by doctest.testmod (), so just run it normally in the cell.

jupyter_notebook


def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(-8, -2)
    -10
    '''
    pass

import doctest
doctest.testmod()

Recommended Posts

Notes on how to use doctest
Notes on how to use pywinauto
Notes on how to use featuretools
How to use Dataiku on Windows
How to use homebrew on Debian
Autoencoder with Chainer (Notes on how to use + trainer)
Notes on how to write requirements.txt
[Hyperledger Iroha] Notes on how to use the Python SDK
Notes on how to use marshmallow in the schema library
How to use Google Assistant on Windows 10
Memorandum on how to use gremlin python
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Python Kivy ④ ~ Execution on Android ~
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to register on pypi
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator