Python Unit Test: Introduction to UnitTest Usage

Source: Internet
Author: User

I. Overview

This article describes the Python unit test framework UnitTest, which is the standard module UnitTest python comes with. UnitTest is designed based on the popular unit testing framework in Java, which is powerful and flexible, and mastering unittest is simple for people familiar with JUnit.

UnitTest involves a lot of knowledge, but the core of those, this article only describes the core and basic content.

Similar to JUnit, use UnitTest to write the unit test code for Python, including the following steps:

1) Write a Python class that inherits the TestCase class from the UnitTest module, which is a test class

2) define the test method (this is the test case ) in the test class written above, and the method name of each method requires a beginning with test with no additional parameters. Calling the test code in the test method, verifying the test results, provides a number of standard validation methods in the TestCase class, such as the most common assertequal.

3) Execute Unittest.main (), the function is responsible for running the test, it instantiates all testcase subclasses, and runs all the methods that begin with test.

Second, Case 1

Let's look at an example below and write the following Python file, for simplicity, we'll put the measured function in a file with the test code:

#Coding=utf-8ImportUnitTest#method of being testeddefCal (A, b):returnA +b#Test Classclasscaltest (unittest. TestCase):defTestA (self): expected= 6result= Cal (2,4) self.assertequal (expected,result)defTestb (self): expected=0 Result= Cal (2,1) self.assertequal (expected,result)#Execute so test codeif __name__=='__main__': Unittest.main ()

The above test class Caltest has two test methods, obviously the Testb method we estimate to let the checksum error. Execute the above python file at the command line and output the following information:

. F

======================================================================

Fail:testb (__main__. Caltest)

----------------------------------------------------------------------

Traceback (most recent):

File "test2.py", line +, in Testb

Self.assertequal (Expected,result)

assertionerror:0! = 3

----------------------------------------------------------------------

Ran 2 Tests in 0.001s

FAILED (Failures=1)

The above shows the use case for error execution and the error message. The last two sentences give the time spent on the entire test execution and the number of cases where the test failed.

If we modify the TESTB correctly, the output of the running result is as follows:

..
----------------------------------------------------------------------
Ran 2 Tests in 0.000s

Iii. Case 2

The above example has only one test file (python file). In a real project, there are often multiple test files, each tested against different business code.

So how do you go about executing the test cases in all the test files?

Suppose we have a test class CalTest1 defined in the test1.py test file, and a test class CalTest2 is defined in another test test2.py test file.

To be able to execute the test cases in both test files at the same time, you can write an overall test file as the total entry for execution:

# Coding=utf-8 Import UnitTest  from Import CalTest1  from Import CalTest2 if __name__= ='__main__': Unittest.main ()  

From the above example, it can be seen that the use of the UnitTest framework is relatively simple and convenient.

Python Unit Test: Introduction to UnitTest Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.