This article is an example of Python unit and document testing. It is shared for your reference as follows:
Unit and Documentation Testing
1. Unit testing
Unit testing is used to check the correctness of a module, a function or a class.
1. If a unit test passes, it proves that the tested function works correctly.
2. The opposite proves that either the function is buggy or the input is not legal, in short we need to fix our function functionality.
2, unit testing of functions
Create a python file with the following contents.
def mySum(x,y): return x + y def mySub(x,y) return x -y print(mySum(1,2))
The python file was created with the following contents:
import unittest from MathFunc import mySum, mySub # Test class Inherited from class Test(): # The following two methods exist: assume that you need to connect to the database, and when the test is complete, you need to disconnect from the database def setUp(self): print("Automatically invoked when starting a test") def tearDown(self): print("Automatically invoked at the end") # Test the corresponding function # In general, test function naming format: text_Name of the function to be tested def test_mySum(self): # Assertion: Name the function Format: text_Name of the function to be tested (mySum(1,2),3,"Addition error.") def test_mySub(self): (mySub(2,1),1,"The subtraction was wrong.") # When the main program is running, start unit tests if __name__ == "__main__": ()
Run the file and find it normal, then modify the contents of the file, which can be modified as follows.
def mySum(x,y): return x + y + 1 def mySub(x, y): return x - y print(mySub(1,2))
Running the file again results in an error message.
3. Unit testing of classes
First create a class file with the following contents:
class Person(object): #constructor def __init__(self, name, age): # Assigning values to member variables = name = age def getAge(self): return
Create a file for unit testing of the class as follows:
import unittest from person import person class Test(): def test_init(self): p = Person('hanmeimei',20) (,"hanmeimei","Attribute values are incorrect.") def test_getAge(self): p = Person('hanmeimei',22) ((),,"The getAge function is wrong.") if __name__ = "__main__": ()
Demonstrate, run the file, the program runs normally, and modify the contents of the file as follows:
class Person(object): #constructor def __init__(self, name, age): # Assigning values to member variables = name = age def getAge(self): return +1
Running it again will report an error
Unit testing of classes: still essentially unit testing of methods.
4、Documentation test
The role of document testing: you can extract the comments to find that code execution
The doctest module extracts the code in the comments for execution.
doctest extracts inputs strictly according to python's interaction mode
import doctest def mySum(x,y): # Describe the function and use of the function ''' Find the sum of two numbers get The sum from x and y :param x:firstNum :param y:secondNum :return sum #Note the spaces example: >>> print(mySum(1,2)) 3 ''' return x + y print(mySum(1,2)) # Perform a documentation test, just do it in the current document ()
Note: The demo is a major test of the
example:
>>> print(mySum(1,2)) 3
For those interested in Python-related content, check out this site's feature: theSummary of Python function usage tips》、《Python Object-Oriented Programming Introductory and Advanced Tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python string manipulation techniques》、《Summary of Python coding manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.