Pytest中引數化詳解
阿新 • • 發佈:2021-08-11
Pytest中引數化詳解1
''' import pytest def add(a, b): return a + b """引數為列表和元組書寫方式一樣""" # @pytest.mark.parametrize( # 'a,b,expect', # [ # [1,2,3], # [3,4,7], # [7,8,15], # ] # [ # (1, 2, 3), # (3, 4, 7), # (7, 8, 15), # ] # ) # def test_add(a, b, expect): # assert add(a, b)==expect """引數為字典的書寫方式""" @pytest.mark.parametrize( 'data', [ {'a':1,'b':2,'expect':3}, {'a':2,'b':3,'expect':5}, {'a':3,'b':4,'expect':7}, ] ) def test_add_01(data): assert add(data['a'], data['b'])==data['expect'] if __name__ == '__main__': pytest.main(['-s', '-v', 'Pytest中引數化詳解(一).py'])'''
Pytest中引數化詳解2
''' import pytest def add(a, b): return a + b @pytest.mark.parametrize( 'a,b,excepts', [ pytest.param(1, 1, 2, id='1'), # id標識如果用中文,無法解碼,最好是用英文及數字表示 pytest.param(2, 2, 4, id='2'), pytest.param(3, 3, 6, id='3'), ] ) def test_add(a, b, excepts): assert add(a, b)==excepts if __name__ == '__main__': pytest.main(['-v', '-s', 'Pytest中引數化詳解(二).py'])while True: print('studying...')'''