Python unit tests

Unit test using doctest module

You can unit test each file by writing as follows (production is skipped)

test.py


#!-*- coding:utf-8 -*-
def double(x):
    """A function that returns 3 times the given number
Unit test the content from here
     
    >>> double(3)
    9
    
Basically you can freely comment">>>"And only the next line is recognized

    >>> double(10)
    30
     
    >>> double(8)
    24
     
    """
    return x * 3
 
if __name__ == "__main__":
    import doctest
    doctest.testmod()

You can unit test by running it with the -v option. (If you do not add -v, nothing will be returned if it ends normally)

python test.py -v
Trying:
    double(3)
Expecting:
    9
ok
Trying:
    double(10)
Expecting:
    30
ok
Trying:
    double(8)
Expecting:
    24
ok
1 items had no tests:
    __main__
1 items passed all tests:
   3 tests in __main__.double
3 tests in 2 items.
3 passed and 0 failed.
Test passed.

Commentary

ʻIf name == "main": only when executed as a script Operate. (Skip when ʻimport test.py is done)

The doctest module allows you to write test code and its results as comments It will test automatically. (It is convenient because it remains as a comment about what kind of check was done)

Recommended Posts

Python unit tests
Write C unit tests in Python
python unit test template
Python
Library path setting to pass GAE / Python local unit tests
Unit test log output with python
Code tests around time in Python
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
Python memo
Python comprehension
Python Singleton
Python basics ④
Python Memorandum 2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
I wrote unit tests for various languages
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
python memorandum
Python memo