In Python, there is a test method called unit test. Its use can bring great benefits to our developers in actual program development. Here we can use the Python unit testing content introduced in this article to have a corresponding understanding of this knowledge.
- How to configure the Python CGI Environment in different operating systems
- Python Algorithm Implementation
- Analysis on the correct use of Python print
- Compile a Python program to calculate the number of rows
- Python SQLITE database is easy to use
Testing is a continuous process throughout the entire development process. In a sense, the software development process is actually a testing process. As Martin Fowler said, "You shouldn't write a program before you know how to test the code. Once you have completed the program, the test code should also be completed. Unless the test is successful, you cannot think that you have compiled a program that can work. "
The basic principle of the test is to compare whether the expected results are the same as the actual execution results. If the results are the same, the test is successful. Otherwise, the test fails. To better understand the role of the PyUnit automated testing framework, let's look at a simple example. Suppose we want to test the Widget class in Example 1:
Example 1. widget. py
- # Classes to be tested
- Class Widget:
- Def _ init _ (self, size = (40, 40 )):
- Self. _ size = size
- Def getSize (self ):
- Return self. _ size
- Def resize (self, width, height ):
- If width 0 or height <0:
- Raise ValueError, "illegal size"
- Self. _ size = (width, height)
- Def dispose (self ):
- Pass
Python programmers who manually perform Python unit tests may write test code similar to Example 2,
Example 2. manual. py
- From widget import Widget
- # Test execution class
- Class TestWidget:
- Def testSize (self ):
- ExpectedSize = (40, 40 );
- Widget = Widget ()
- If widget. getSize () = expectedSize:
- Print "test [Widget]: getSize works perfected! "
- Else:
- Print "test [Widget]: getSize doesn't work! "
- # Test
- If _ name _ = '_ main __':
- MyTest = TestWidget ()
- MyTest. testSize ()
It is not difficult to find that this manual testing method has many problems. First of all, the writing of the test program does not follow certain standards. Ten programmers may write ten different test programs, if every Python programmer has his/her own methods for designing test classes, it would be too much trouble to maintain the classes tested. Second, you need to write a lot of auxiliary code to perform unit testing. In example 1, the code used for testing is even more than the code to be tested, which will undoubtedly increase the workload of Python programmers.
In order to make unit test code easier to understand by testing and maintenance personnel, the best solution is to let developers follow certain specifications to compile the code for testing, for Python programmers, PyUnit is used to construct unit test cases. Currently, PyUnit has been recognized by most Python developers and has become the de facto Python unit testing standard. If PyUnit is used for the same test, the test code is shown in Example 3:
Example 3. auto. py
- From widget import Widget
- Import unittest
- # Test execution class
- Class WidgetTestCase (unittest. TestCase ):
- Def setUp (self ):
- Self. widget = Widget ()
- Def tearDown (self ):
- Self. widget = None
- Def testSize (self ):
- Self. assertEqual (self. widget. getSize (), (40, 40 ))
- # Construct a Test Set
- Def suite ():
- Suite = unittest. TestSuite ()
- Suite. addTest (WidgetTestCase ("testSize "))
- Return suite
- # Test
- If _ name _ = "_ main __":
- Unittest. main (defaultTest = 'suite ')
After adopting the Python unit test framework, the code used for testing has been changed accordingly:
Use the import statement to introduce the unittest module.
Let all the classes that run the test inherit from the TestCase class. You can regard TestCase as a set of methods for testing a specific class.
Initialize the task before the test in the setUp () method, and clear the task after the test in the tearDown () method, setUp () and tearDown () all are methods defined in the TestCase class.
Call the assertEqual () method in testSize () to compare the returned value of the getSize () method and the expected value in the Widget class to ensure that the two values are equal. assertEqual () it is also defined in the TestCase class.
Provides a global method named suite (). PyUnit calls the suit () method during the test execution to determine how many test cases need to be executed, we can regard TestSuite as a container that contains all test cases.
Although it looks a bit complicated, PyUnit allows all Python programmers to use the same Python unit test method, and the test process is no longer messy, but ordered behavior under the guidance of the same specification, which is the biggest benefit of using the PyUnit automated unit test framework.