Basics of unit testing framework Pytest (Mushishi "selenium3 automated testing combat - based on the Python language note 42")

1. Install

DOS line installation:

python -m pip install pytest

Note: Before running pytest script on pycharm, pycharm need to modify the configuration:

Configuration file --- --- Tool --- Default test runner: revised to py.test

 2.Pytest assertion

Import pytest 


# Function: for calculating the sum of a and b and 
DEF the Add (a, b):
     return a + b 


# Function: means for determining prime 
DEF is_prime (n-):
     IF n-<=. 1 :
         return False
     for I in Range (2 , n-):
         IF n-I% == 0:
             return False
         return True 


# test for equality 
DEF test_add_1 ():
     Assert the Add (. 3,. 4). 7 == # test unequal DEF test_add_2 ():
     Assert



! the Add (. 17, 22 is) = 50 # test greater than or equal DEF test_add_3 ():
     Assert the Add (. 17, 22 is) <= 50 # test less DEF test_add_4 ():
     Assert the Add (. 17, 22 is)> = 38 is # test comprising DEF test_in (): 
    A = ' Hello ' 
    B = ' of He ' Assert B in A # test does not contain DEF test_not_in (): 
    A = ' Hello ' 
    B = ' Hi ' Assert












    




    B Not  in A 


# determines whether the True 
DEF test_true_1 ():
     Assert is_prime (13 is ) 


# determines whether the True 
DEF test_true_2 ():
     Assert is_prime (. 7) IS True 


# determines whether or not True 
DEF test_true_3 ():
     Assert  Not is_prime (4 ) 


# determines whether or not True 
DEF test_true_4 ():
     Assert is_prime (. 6) iS  not True 


# determines whether False 
DEF test_false_1 ():
     Assert is_prime (. 8) iS False

if __name__ == '__main__':
    pytest.main()

operation result:

 

 

3.Fixture

Test methods used to test the function, the test class file to initialize and test the entire test environment or reducing

Import pytest 


# performance function 
DEF Multiply (A, B):
     return A * B 


class TestMultiply:
     # ========= ======== Fixture 
    @classmethod
     DEF setup_class (CLS):   # current started test class 
        Print ( " setup_class ==========> " ) 

    DEF teardown_class (CLS):   # at the end of the execution of the current test class 
        Print ( " teardown_class ========== > " ) 

    DEF setup_method (Self, method,):   # begin in each test method 
        Print ( "------ setup_method> " ) 

    DEF teardown_mehod (Self, Method,):   # at the end of each test method execution 
        Print ( " teardown_method ------> " ) 

    DEF Setup (Self):   # of each test prior to the implementation of the method, equivalent to setup_method () 
        Print ( " Setup ----> " ) 

    DEF tearDown (Self):   # executed after each test method is equivalent to teardown_method (function) 
        Print ( " tearDown --- ---> " ) 

    # ========= test ======== 
    DEF test_multiply_3_4 (Self):
         Print ( "test_number_3_4")
        assert multiply(3, 4) == 12

    def test_multiply_a_3(self):
        print("test_string_a_3")
        assert multiply('a', 3) == 'aaa'

if __name__ == '__main__':
    pytest.main()

 Operating results as follows:

 

4. parameterized

Import pytest
 Import Math 


# pytest parametric 
@ pytest.mark.parametrize (
     " Base, exponent The, expected " ,   # custom parameter name 
    [(2, 2, 4 ), 
     ( 2, 3, 8 ), 
     ( 1, 9, 1 ), 
     (0, . 9 , 0)], 
    IDS = [ " Case1 " , " Case2 " , " Case3 " , " Case 4 " ]   # define test name 
    )
 DEF test_pow(base, exponent, expected):
    assert math.pow(base, exponent) == expected


if __name__ == '__main__':
    pytest.main()

operation result:

 

Guess you like

Origin www.cnblogs.com/kite123/p/11592207.html