python + unittest framework

A, unittest framework for understanding

TestCase: representation of test cases

TestSuite: indicates the test set, here a set of multiple test cases

TestLoader: End use cases set waiting to be tested

runner: represents examples of these operated with wait tests

result: Test results indicate return

 

 

Two, demo

1, create a general method (in case you need to call the test method)

Math.py

class  Math():
    def add(self,x,y):
        return x+y

    def sub(self,x,y):
        return x-y

2, create test cases

testApi.py

Import the unittest
 from Test.Math Import the Math 

# write test classes used for writing test 
class test_api (of unittest.TestCase):
     DEF the setUp (Self,): 
        self.x = 10 
        self.y = 20 is DEF test_add (Self): 
        Result = the Math () the Add (self.x, self.y).
         Print ( " addition " , Result) DEF test_sub (Self): 
        Result = . the Math () Sub (self.x, self.y)
         Print ( " subtraction " , the Result) DEF

    

    

     tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main

 

3, execution cases

executeCase.py

# The Author: LS Liu 
Import the unittest 

# created with Example collector 
Suite = unittest.TestSuite 

# introducing a specific embodiment where the module name 
from the Test Import TestAPI 

# created with Example loader 
TS = unittest.TestLoader () 
suite.addTests (ts.loadTestsFromModule (TestAPI) ) 

# execution Example 
Runner = unittest.TextTestRunner () 
runner.run (Suite)

 

Several methods of collection of test cases:

Method a: loading a use case

suite.addTest(test_api("test_add"))

Method two: loading a plurality of use cases

suite.addTests(test_api("test_add"),test_api("test_sub"))

Method three: loaded from the specified module

ts = unittest.TestLoader () # Create a loader Example

suite.addTests (ts.loadTestsFromModule (testApi.py)) # pass the module name

Method four: loaded with the test cases from the specified class

ts = unittest.TestLoader () # Create a loader Example

suite.addTests (ts.loadTestsFromModule (testApi.py)) # incoming class name

 

 

 

 



Guess you like

Origin www.cnblogs.com/Jungle1219/p/12667438.html