Python Unit Test UnitTest

Source: Internet
Author: User

Unit testing can help us locate the module and the unit that is having problems accurately and quickly.

The object of the unit test is a function, which must have the output (even if it is an abnormal output) so that the unit test module can capture the return value and compare it to the expected value to get the test pass or not

Unit test loading can be done by Unittest.main () to start the test module of the unit test.

The use cases for unit tests are stored in testsuit, and the 2 methods are listed below.

    • SetUp (): Run before each test function runs
    • TearDown (): Executes after each test function finishes running
    • SetupClass (): Must use @classmethod adorner, all test runs before run
    • Teardownclass (): Must use @classmethod adorner, run once after all test runs

Example 1: Test function

def sum (x, y):     return x + ydef  Sub (x, y):    return XY

Unit Test Module

classmytest (unittest. TestCase): #所有执行测试的类都继承TestCase类#Initialization Work    defsetUp (self):Pass    #Repaying work    defTearDown (self):Pass    #For specific test cases, be sure to start with test    deftestsum_1 (self): self.assertequal (SUM (1, 2), 3,"test sum fail")    deftestsum_2 (self): self.assertequal (SUM (1, 2), 2,"test sum fail")    deftestsub (self): self.assertequal (Sub (2, 1), 1,"Test Sub Fail")if __name__=="__main__": Unittest.main ()

Test results

.. F======================================================================fail:testsum_2 (__main__.mytest)-------- --------------------------------------------------------------Traceback (most recent):  File "e:/task/ ??????? /test1.py ", line testsum_2    self.assertequal (SUM (1, 2), 2," Test sum fail ") Assertionerror:test sum fail-------- --------------------------------------------------------------Ran 3 Tests in 0.003sFAILED (Failures=1)

Note : F indicates fail, the point before F indicates the test passed, and E indicates that the program itself is abnormal

Example 2: Test class

class MyClass:     def __init__ (self):         Pass    def sum (self, x, y):         return x + y    def  Sub (self, x, y):        return XY

Test code

classmytest (unittest. TestCase):defsetUp (self): Self.tclass=MyClass ()defTearDown (self):Pass    deftestsum_1 (self): self.assertequal (Self.tclass.sum (1, 2), 3,"test sum fail")    deftestsum_2 (self): self.assertequal (Self.tclass.sum (1, 2), 3,"test sum fail")    deftestsub (self): self.assertequal (Self.tclass.sub (2, 1), 1,"Test Sub Fail")if __name__=="__main__": Unittest.main ()

Results

.. F======================================================================fail:testsum_2 (__main__.mytest)-------- --------------------------------------------------------------Traceback (most recent):  File "e:/task/ ???????  /test2.py ", line testsum_2    self.assertequal (self.tclass.sum (1, 2), 5," Test sum fail ") Assertionerror:test sum Fail----------------------------------------------------------------------Ran 3 tests in 0.001sFAILED (Failures=1)

Example 3: Constructing a Test set

Code under test

class MyClass:     def __init__ (self):         Pass    def sum (self, x, y):         return x + y    def  Sub (self, x, y):        return XY

Test code

classmytest (unittest. TestCase):defsetUp (self): Self.tclass=MyClass ()defTearDown (self):Pass    deftestsum_1 (self): self.assertequal (Self.tclass.sum (1, 2), 3,"test sum fail")    deftestsum_2 (self): self.assertequal (Self.tclass.sum (1, 2), 5,"test sum fail")    deftestsub (self): self.assertequal (Self.tclass.sub (2, 1), 1,"Test Sub Fail")defSuite (): Suite=UnitTest. TestSuite () suite.addtest (MyTest ("testsum_1")) Suite.addtest (MyTest ("testsum_2")) Suite.addtest (MyTest ("TestSub"))    returnSuiteif __name__=="__main__": Unittest.main (defaulttest="Suite")

Python Unit Test UnitTest

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.