The unit test process in Python

Source: Internet
Author: User

The unit test process in Python

Looked at the unit test flow in Python, wrote a test code to see the overall testing process

Summarized as follows

    • A test case class should derive from UnitTest. TestCase
    • The normal test case is called in the order

    • Subclass TestCase can register its own cleanup function (My_cleanup). This cleanup function will be called after teardown
    def setup (self):        super (Mytestcase, self). Setup ()        self.addcleanup (Self.my_cleanup)
    • Unit test function Name must start with "Test_"
    def test_case_1 (self):        print ("++++ test Case 1")


Test the source code:

From __future__ import print_functionimport unittestdef setupmodule ():    print ("Setupmodule") def teardownmodule ():    print ("Teardownmodule")    class Mytestcase (unittest. TestCase):    def setup (self):        super (Mytestcase, self). Setup ()        self.addcleanup (self.my_cleanup)                Print ()        print ("++++ setup")    def TearDown (self):        super (Mytestcase, self). TearDown ()        print ("++++ TearDown ")        raise Exception ()            def my_cleanup (self):        print (" ++++ my_cleanup ")            def test_case_1 ( Self):        print (' ++++ test Case 1 ')    def test_case_2 (self):        print ("++++ test Case 2")              @classmethod    def setupclass (CLS):        print ("SetupClass")    @classmethod    def teardownclass (CLS):        print ()        print ("Teardownclass")


The running result of this program:

setupmodulesetupclass++++ setup++++ test Case 1++++ teardown++++ my_cleanup++++ setup++++ test Case 2++++ tearDown++++ my_ Cleanupteardownclassteardownmodule



The unit test process in Python

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.