Python, how to unit test

Since it is a test, then we have to have the code being tested, we first define a simple function, the function of this function is to receive a name, and return a greeting statement.

say_hello_function.py

1 def hello_name(name):
2     greet = "Hello , "
3     return greet + name.title()

Clearly, unit testing Python also provides us with the appropriate module (unittest), unit testing: used to determine the function of certain areas is no problem; the test case: that is, a set of unit tests, behavior in each case are in line with our expected.

First, successfully passed the test

. Step write tests: a first import unittest module; Create a class 2 (any name) Inheritance unittest.TestCase; 3 need to write code to test the various aspects of the function test... Let's just use the above example of a function that is only a test case contains a method is consistent with our expectations after the main checking function hello_name run.

test_say_hello.py

 

1  # Coding = gbk // because the editor I use for encoding GBK, so the need to add this line, if you are utf-8, utf-8 will gbk changed to (mainly to allow Python to identify and comment Chinese character string) 
2  Import the unittest
 . 3  
. 4  from say_hello_function Import hello_name
 . 5  
. 6  class HelloTest (of unittest.TestCase):
 . 7      "" " for testing say_hello_function.py " "" 
. 8      DEF test_hello_name (Self):
 . 9          "" " whether the name correctly handle Joker "" " 
10          the hello_str = hello_name ( ' Joker ' )
 11          self.assertEqual (the hello_str, 'Hello , Joker')
12         
13 unittest.main()

 

Explanation: international practice, first import unittest module and function hello_name to be tested (), line 6 code, we created a class called HelloTest and inherited unittest.TestCase class, although the class name you can easily take but people see the best-known name meaning, such as the class name I taken included test, people know that this is a very simple test class, class inheritance unittest.TestCase this is necessary not at liberty, because it will Python We know how to run the test you write.

This class contains only a method for testing hello_name (a function of the aspect). We give method command test_hello_name (), Oh, this method name can not be arbitrarily taken, it must be the beginning of test_, so Python to run all the way to the beginning of test_ at run time.

Line 10 we call the hello_name () function and the function returns the result stored in the variable hello_str, we are in the first 11 lines of code, use the unittest one of the most useful methods: assert methods. Assertion is obvious, it is used to verify whether the result obtained is consistent with our expectations. Here we know hello_name () function returns Hello +, (comma) + name (with initial capital letters), there is a space between words and symbols, so we expected a "Hello, Joker", in order to verify what we think we call that is unnittest method assertEqual () and pass it two parameters hello_str and "Hello, Joker", the value of the latter will hello_str strings are compared, if they are equal, then no problem, but if not equal , the thing is big.

We run the test, the console is printed as follows:

The first line has a point represents a test passes, the dashed line represents the following line, Python run a test, the elapsed time is less than .001 seconds, and the last row indicates OK all use cases are passed. To sum up, when to the function hello_name () pass in a name for the "Joker", the function can always handled correctly. After modifying the function, run the test again, if it passes, we know that when given the name Joker, this function can still be processed correctly.

 

  To be continued ......  

 

  Do you think they are very rich people happy? Right, their happiness, you can not imagine.

  

 

Guess you like

Origin www.cnblogs.com/tizer/p/11080007.html