Python unittest use

Source: Internet
Author: User
Tags class definition

Uinittest use UnitTest. TestCase

Instance:

import unittestclass Test1(unittest.TestCase):    def test_x1(self):        self.a=1    def test_x2(self):        print("a:",self.a)class Test2():    def x1(self):        self.b=1    def x2(self):        print("b:",self.b)#测试调用1unittest.main()#测试调用2A=Test2()A.x1()A.x2()
    • First UnitTest. The test case method in TestCase needs to start with test, and subsequent calls to Unittest.main () will automatically execute these use-case methods
    • Unittest.main () is a function that executes the functions in the Test1 class sequentially, but it differs from the normal class instance by invoking the method individually.
      The obvious test call 1 executes the test_x1 and the Test_x2 method one by one, when executing to the TEST_X2, the error does not define the variable a
      The Test call 2 does not report that the variable B is undefined because the variable b is initialized when the a.x1 () is executed. So there's no error.
TestSuite
    • Use the above class Test1 (UnitTest. TestCase) class definition
suite=unittest.TestSuite()suite.addTest(Test1('test_x1'))suite.addTest(Test1('test_x2'))#或者使用#suite.addTests([Test1('test_x1'),Test1('test_x2')]) 相当于上面两句runner = unittest.TextTestRunner()runner.run(suite)

Python unittest use

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.