Studies of heavy python unittest framework

Re-learn unittest framework.

Automated steps:

1: write use cases TestCaase, all use cases have been test_ beginning, a case is a function, can not be mass participation, only the self keyword.

2: Write assert Assert, comparison of actual results and expected results

3: Example execution. 1: storing TestSuite Example 2: TestLoader find use cases, loaded with the embodiment, is stored inside TestSuite

4: a test report HTMLTestRunner

5: When the mouse operation is not misplaced, preferably at the beginning and end of discharge mouse, preventing run only single use case.

usage:

Test Objective: Test of addition and multiplication:

class TestMethod ():
     # write Use Case 
    # : a use case is a function that can not pass argument, only the self keyword 
    # : All use cases have been test_ beginning 
    DEF  __init__ (self, A, b): 
        self.a = A 
        self.b = B 

    DEF the Add (Self):
         '' ' adding ' '' 
        return self.a + self.b, 

    DEF Multi (Self):
         '' ' multiplication ' '' 
        return self.a * self.b,

Test cases and assertion:

from TaceCase.math_method Import TestMethod      # TODO test target class 
Import the unittest 


# . 1: written Example TestCaase 
# 2: execution Example. 1: TestSuite storing Example 2: TestLoader find use cases, loaded with the embodiment, is stored TestSuite inside 
# 3: comparison of actual results and expected results assert the assert 
# 4: test report htmltestrunner 


class TestMathMethod (unittest.TestCase):
     '' ' test of mathematical methods ' '' 

    # written in Example 
    # : a use case is a function that can not pass argument, only the self keyword 
    # : all use cases test_ already beginning 
    # : runtime cursor can not be prosecuted, it is best to put the mouse and the beginning of the end of 
    DEF the setUp (Self):
         '' ' used to do the preparatory work ' '' 
        Print (' Begins execution Example ' ) 

    DEF test_add_two_positive (Self):
         '' ' test two positive numbers together ' '' 
        Result = TestMethod (3,3 ) .add ()
         Print ( " result value is 3 + 3: " , Result)
         # TODO asserts than expected and actual values, through agreement on, failure is inconsistent 
        self.assertEqual (. 6, Result, ' . 3 + miscalculated. 3 ' ) 

    DEF test_add_two_zero (Self):
         '' ' test 2 adding 0 '' ' 
        Result = TestMethod (0,0) .add ()
         Print ( "Results 0 + 0 is: " , Result)
        self.assertEqual (0, Result) 

    DEF test_add_two_negative (Self):
         '' ' test negative adding 2 ' '' 
        Result = TestMethod (-3, -3 ) .add ()
         Print ( " Results -3 + -3 value: " , Result) 
        self.assertEqual ( -6 , Result) 

    DEF the tearDown (Self):
         '' ' used for cleaning the data ' '' 
        Print ( ' complete with Example execution ' ) 

IF  the __name__ == ' __main__ ' :
     ' ''unittest execution order of encoding to ascii, az execution order of '' '
    unittest.main()

The implementation of use cases:

from TaceCase.unit_testing Import TestMathMethod
 Import the unittest
 Import HTMLTestRunner 



Suite = unittest.TestSuite ()   # TODO instantiate an object, storing in Example 

# 1 
# execute a single use case, transfer cases use (function) Title 
# suite.addTest (TestMathMethod.test_add_two_positive) 

# Method 2 TestLoader Create a loader 
Loader = unittest.TestLoader ()
 # class name passed TODO performed 
suite.addTest (loader.loadTestsFromTestCase (TestMathMethod)) 

# methods. 3 
from TaceCase Import unit_testing
 # .py TODO pass execution module name 
#suite.addTest(loader.loadTestsFromModule(unit_testing))

# 执行
runner = unittest.TextTestRunner()
runner.run(suite)

testing report:

# 出html报告
with open('../report.html','wb') as f:
     runner = HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=1, title="测试报告")
     runner.run(suite)

 

Guess you like

Origin www.cnblogs.com/xiamaojjie/p/11785679.html