One of the methods to test functions in Python is to test functions in python.
One of the methods to test functions in Python
First, we will introduce the simple try... try t, such as the following figure and code:
Note: The following code environment is 2.7.x. print the printed content in brackets)
#!/usr/bin/env python# -*-coding:utf-8 -*-def test(x,y): print x+ytry: test()except: print 'Error'test(1,2)
The function tested by test does not include x, y. The result of the function is the sum of x and y.
Try: Execute the test () function. Normally, execute the function and print the sum of x and y.
Warning T: otherwise, 'error' is printed. Because test does not define parameters for x and y, it will go to warning T.
The significance of this method is that if you have made a dish and you do not know whether the taste is salty or light or normal, then we will try it. If it is normal, it will not be added.
It's not normal. You know what it means, too. Let's take a look at the results:
Of course, you can also execute multiple functions and conditions for testing, so that you can automate the test of business code, you can also interspersed in the places you need in the program
Check and so on, so that your program will follow the process, and this piece can get the test results you want. The Code is as follows:
#!/usr/bin/env python# -*-coding:utf-8 -*-x = 0lis_y = [i for i in range(64,90)]def test(x,y): print x+y," ",def test1(x,y): print x*ytry: while x < 20: for y in lis_y: test(x,y) test1(x,chr(y)) x+=1except: print 'Error'
This is a bit complicated, because I used the lis_y list analytical expression to get the final list of 64-89 through a for loop range,
The test1 function calculates the sum of x and y, and the test1 function calculates the multiplication result of x and y.
A while LOOP of condition x <20 is added, and then y is used to take values from list y, test addition, and add the chr method to test multiplication.
The chr method returns the ASCII characters corresponding to the integer I. Opposite to ord. Let's see the running result:
The output result is not very interesting. The comprehension of this result requires learning several modules and loop methods.
We can see that the students here are still very good. As long as you practice more, you will be able to make a difference, and you will be able to make a new change. As for whether this change is a variant or something, you can see it as a result.
Thanks for watching. We will continue to update it later.