Python unit testing framework - unittest

1. How Pyhon works - core concepts: test case, testsuite, TestLoder, TextTestRunner, TextTestResult, test fixture

TestCase (test case):  The base class of all test cases, it is the most basic unit in software testing.

                   A test case is a test case, which is a complete test process, including the setup of the pre-test environment setUp, the execution of the test code (run), and the restoration of the post-test environment (tearDown). A test case is a complete test unit that can be used to verify a problem.

TestSuite (test suite): Multiple test case test case collection is TestSuite, TestSuite can nest TestSuite

TestLoder: It is used to load TestCases into TestSuite. There are several loadTestsFrom_() methods, which are to find TestCases from various places, create their instances, add them to TestSuite, and return a TestSuite instance.

TextTestRunner: It is used to execute test cases, in which run(test) will execute the run(result) method in TestSuite/TestCase.

 TextTestResult: The test result will be saved to the TextTestResult instance, including how many use cases have been run, how many successes and failures, etc.

TestFixture: Also known as test scaffolding, the running environment of the test code, refers to the work to be done before and after the test preparation, including the setUp and tearDown methods

Second, the test process:

1. Write TestCase: A class inherits unittest.TestCase, which is a test test case, which has multiple methods starting with test, then each such method will generate a TestCase instance when it is loaded. If there are four methods starting with test in a class, there are four test cases when finally loaded into the suite

2. Load TestCase to TestSuite by TestLoder

3. Then the TestSuite is run by TextTestRunner, and the result of the operation is stored in TextTestResult.

Description: a: When executed through the command line or unittest.main(), main will call run in TextTestRunner to execute, or you can execute the use case directly through TextTestRunner

b: When the Runner is executed, the result is output to the console by default, we can set it to output to a file, view the result in the file, or output the result to HTML through HTMLTestRunner)

Three, unittest example:

1. Prepare the method to be tested:

mathfunc.py

2. Write a test for the above method:

This is a simple test with a few points to note:

       a: The first line gives the identification of the result of each use case execution, success is  ., failure is  F, error is  E, skip is  S. It can also be seen from the above that the execution of the test has nothing to do with the order of the methods. test_divide is written in the fourth, but it is executed in the second.

        b: Each test method  test starts with, otherwise it is not recognized by unittest.

        c: Adding parameters to unittest.main()  verbosity can control the detail of the output error report. The default is  1, if set to  0, the execution result of each use case will not be output, that is, there is no line 1 in the above result; if If set to  2, output detailed execution results

3. Organizing TestSuites

        a: Determine the order of test cases, which will be executed first and which will be executed later?

         b: If there are multiple test files, how to organize them?

TestLoder loads TestCase several methods

4. Generate the results to a file:

5. test fixture——setUp(),tearDown()方法

setUp() And  tearDown() two methods (in fact, these two methods of TestCase are rewritten), these two methods are executed once before and after each test method is executed, setUp is used to prepare the environment for the test, tearDown is used to clean up the environment, has Prepare for subsequent testing.

 

class TestMathFunc(unittest.TestCase): 

# The setUp and tearDown methods will be executed once before and after each test method is executed, setUp is used to prepare the environment for the test, tearDown is used to clean up the test after the environment is prepared
def setUp(self) :
print 'do something before test.prepare environment'

def tearDown(self):
print 'do something after test.clean up'If
you want to prepare the environment before all cases are executed, and clean up the environment after all cases are executed, We can use  setUpClass() with tearDownClass():
class TestMathFuncTwo(unittest.TestCase): #If 

you want to prepare the environment before all cases are executed, and clean up the environment after all cases are executed, we can use setUpClass() and tearDownClass(). Note: @classmethod must be added, Otherwise, an error will be reported
@classmethod
def setUpClass(cls):
print 'This setUpClass() method only called once'

@classmethod
def tearDownClass(cls):
print 'This tearDownClass() method only called once too'
6. Skip a case

7. Output HTML report with HTMLTestRunner

HTMLTestRunner is a third-party unittest HTML report library. First, we download HTMLTestRunner.py and put it in the current directory, or under your 'C:\Python27\Lib', you can import and run it

ps: Some of the content of this article is taken from the Internet, link: http://blog.csdn.net/huilan_same/article/details/52944782

TestCase (test case):  The base class of all test cases, it is the most basic unit in software testing.

                   A test case is a test case, which is a complete test process, including the setup of the pre-test environment setUp, the execution of the test code (run), and the restoration of the post-test environment (tearDown). A test case is a complete test unit that can be used to verify a problem.

TestSuite (test suite): Multiple test case test case collection is TestSuite, TestSuite can nest TestSuite

TestLoder: It is used to load TestCases into TestSuite. There are several loadTestsFrom_() methods, which are to find TestCases from various places, create their instances, add them to TestSuite, and return a TestSuite instance.

TextTestRunner: It is used to execute test cases, in which run(test) will execute the run(result) method in TestSuite/TestCase.

 TextTestResult: The test result will be saved to the TextTestResult instance, including how many use cases have been run, how many successes and failures, etc.

TestFixture: Also known as test scaffolding, the running environment of the test code, refers to the work to be done before and after the test preparation, including the setUp and tearDown methods

Second, the test process:

1. Write TestCase: A class inherits unittest.TestCase, which is a test test case, which has multiple methods starting with test, then each such method will generate a TestCase instance when it is loaded. If there are four methods starting with test in a class, there are four test cases when finally loaded into the suite

2. Load TestCase to TestSuite by TestLoder

3. Then the TestSuite is run by TextTestRunner, and the result of the operation is stored in TextTestResult.

Description: a: When executed through the command line or unittest.main(), main will call run in TextTestRunner to execute, or you can execute the use case directly through TextTestRunner

b: When the Runner is executed, the result is output to the console by default, we can set it to output to a file, view the result in the file, or output the result to HTML through HTMLTestRunner)

Three, unittest example:

1. Prepare the method to be tested:

mathfunc.py

2. Write a test for the above method:

This is a simple test with a few points to note:

       a: The first line gives the identification of the result of each use case execution, success is  ., failure is  F, error is  E, skip is  S. It can also be seen from the above that the execution of the test has nothing to do with the order of the methods. test_divide is written in the fourth, but it is executed in the second.

        b: Each test method  test starts with, otherwise it is not recognized by unittest.

        c: Adding parameters to unittest.main()  verbosity can control the detail of the output error report. The default is  1, if set to  0, the execution result of each use case will not be output, that is, there is no line 1 in the above result; if If set to  2, output detailed execution results

3. Organizing TestSuites

        a: Determine the order of test cases, which will be executed first and which will be executed later?

         b: If there are multiple test files, how to organize them?

TestLoder loads TestCase several methods

4. Generate the results to a file:

5. test fixture——setUp(),tearDown()方法

setUp() And  tearDown() two methods (in fact, these two methods of TestCase are rewritten), these two methods are executed once before and after each test method is executed, setUp is used to prepare the environment for the test, tearDown is used to clean up the environment, has Prepare for subsequent testing.

 

class TestMathFunc(unittest.TestCase): 

# The setUp and tearDown methods will be executed once before and after each test method is executed, setUp is used to prepare the environment for the test, tearDown is used to clean up the test after the environment is prepared
def setUp(self) :
print 'do something before test.prepare environment'

def tearDown(self):
print 'do something after test.clean up'If
you want to prepare the environment before all cases are executed, and clean up the environment after all cases are executed, We can use  setUpClass() with tearDownClass():
class TestMathFuncTwo(unittest.TestCase): #If 

you want to prepare the environment before all cases are executed, and clean up the environment after all cases are executed, we can use setUpClass() and tearDownClass(). Note: @classmethod must be added, Otherwise, an error will be reported
@classmethod
def setUpClass(cls):
print 'This setUpClass() method only called once'

@classmethod
def tearDownClass(cls):
print 'This tearDownClass() method only called once too'
6. Skip a case

7. Output HTML report with HTMLTestRunner

HTMLTestRunner is a third-party unittest HTML report library. First, we download HTMLTestRunner.py and put it in the current directory, or under your 'C:\Python27\Lib', you can import and run it

ps: Some of the content of this article is taken from the Internet, link: http://blog.csdn.net/huilan_same/article/details/52944782

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325287014&siteId=291194637