class TestCalc: @classmethod def setup_class(cls): print("setup_class") def setup(self): print("setup") self.calc = Calc() def teardown(self): print("teardown") def teardown_class(self): print("teardown_class") test_data_add = [(1, 2, 3), (-1, -2, -3), (0.1, 0.2, 0.3), (1000, 2000, 3000), (1, -1, 0), (1, 0.1, 1.1), (0, 0, 0), (0, 1, 1), (0, -1, -1)] test_data_div = [(1, 2, 0.5), (-1, -2, 0.5), (0.1, 0.2, 0.5), (1000, 2000, 0.5), (4, 2, 2), (-4, 2, -2)] @pytest.mark.parametrize("a,b,result", test_data_add) def test_add(self, a, b, result): assert self.calc.add(a, b) == result @pytest.mark.parametrize("a,b,result", test_data_div) def test_div(self, a, b, result): assert self.calc.div(a, b) == result def zatest_div_exception(self): with pytest.raises((ZeroDivisionError)): self.calc.div(1, 0)
class TestCalc: def setup_class(self): self.calc = Calc() @pytest.mark.parametrize("a, b, result", test_data_add) def test_add(self, a, b, result): assert self.calc.add(a, b) == result @pytest.mark.parametrize("a, b, result", test_data_div) def test_div(self, a, b, result): assert self.calc.div(a, b) == result def test_div_exception(self): with pytest.raises(ZeroDivisionError): self.calc.div(3, 0)
class TestCalc(): def setup_class(self): self.calc = Calc() @pytest.mark.parametrize("a,b,result", test_add_date) def test_add(self, a, b, result): assert self.calc.add(a, b) == result @pytest.mark.parametrize("a,b,result", test_div_date) def test_div(self, a, b, result): if b == 0: with pytest.raises(ZeroDivisionError): self.calc.div(a, b) else: assert self.calc.div(a, b) == result
class TestCalc: # @pytest.mark.parametrize() def setup(self): self.calc = Calc() @pytest.mark.parametrize("a,b,expect", [(0, 0, 0), (1, 2, 3), (-1, 1, 0)]) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert expect == result @pytest.mark.parametrize("a,b,expect", [(0, 1, 0), (2, 1, 2), (-1, 1, -1), (2, 0, 'ZeroDivisionError'), (2, 3, 2 / 3)]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert expect == result @pytest.mark.skipif(sys.version_info < (3, 8), reason='版本小于3.8不跑') @pytest.mark.parametrize('a', [1, 2, 3, 4, 6, 8]) @pytest.mark.parametrize('b', [48, 24, 16, 12]) def test_multiplication(self, a, b): result = self.calc.multiplication(a, b) assert result == 48 def test_subtraction(self): result = self.calc.subtraction(2, 4) assert result == -2
class Testcalc: def setup(self): self.calc = Calc() #获取正常情况 @pytest.mark.add @pytest.mark.parametrize("a,b,expected", Data1.get_data("norm")) def test_add_1(self, a, b, expected): result = self.calc.add(a, b) assert expected == result #获取异常情况 @pytest.mark.parametrize("a,b,res", Data1.get_data("expect")) @pytest.mark.xfail def test_add_2(self, a, b, res): result = self.calc.add(a, b) @pytest.mark.parametrize("data1,data2,data3", [(3, 3, 1), (2, 2, 1)]) def test_div_1(self, data1, data2, data3): try: result = self.calc.div(data1, data2) except ZeroDivisionError: return "division by 0" assert result == "division by 0" assert data3 == result
class TestCalc: @pytest.fixture() def db(self): self.calc = Calc() # def setup(self): # self.calc = Calc() @pytest.mark.parametrize('test_date',add_data.get_data('norm')) def test_add_1(self,db, test_date): result = self.calc.add(test_date['a'], test_date['b']) print(result) assert test_date['c'] == result @pytest.mark.parametrize('test_date', add_data.get_data('except')) def test_add_2(self, db, test_date): with pytest.raises(TypeError): self.calc.add(test_date['a'], test_date['b']) @pytest.mark.finished @pytest.mark.parametrize('a,b,c', [(2, 1, 2), (-1, 1, -1), (999, 1, 999), (-1, -2, 0.5), (0.1, 0.1, 1), (-0.1, 1, -0.1)]) def test_div(self,db,a,b,c): result = self.calc.div(a, b) print(result) assert c == result
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.run(order=-1) def test_add(self): assert self.calc.add(1, 2) == 3 def test_div(self): assert self.calc.div(1, 2) == 0.5 def test_params(self): data = (1, 2) self.calc.add2(data) self.calc.add(*data) @pytest.mark.run(order=1) def test_zadd(self): assert self.calc.add(1, 2) == 3 def test_1add(self): assert self.calc.add(1, 2) == 3 @pytest.mark.demo @pytest.mark.parametrize("a,b", [(1, 2), (2, 3), (3, 4)]) def test_parmas1(self, a, b): data = (a, b) self.calc.add2(data)
class TestCalc: #类初始化执行一次 @classmethod def setup_class(cls): print("setup_class") def setup_method(self): print("setup_method") def setup(self): self.calc = Calc() print("setup") def test_add(self): assert self.calc.add(1, 2) == 3 def test_div(self): assert self.calc.div(1, 2) == 0.5 def test_paras(self): data = {"a": 1, "b": 2} # self.calc.add2(data) self.calc.add(**data) def teardown(self): print("teardown") def teardown_method(self): print("teardown_method") def teardown_class(self): print("teardown_class")
class TestOrder: @classmethod def setup_class(cls): print("setup_class") def setup_method(self): print("setup_method") def setup(self): print("setup") self.calc = Calc() def teardown(self): print("teardown") def teardown_method(self): print("teardown_method") @pytest.mark.run(order=-1) def test_zadd(self): print("add") assert self.calc.add(1, 2) == 3 @pytest.mark.run(order=1) def test_div(self): print("div") assert self.calc.div(1, 2) == 0.5 @pytest.mark.parametrize("a, b", [(1, 2), (2, 3), (3, 4)]) def test_params(self, a, b): print("params") data = (a, b) self.calc.add2(data) self.calc.add(*data)
class TestCalc: def setup(self): self.calc = Calc() # @pytest.mark.parametrize("x,y",[ # (3+5,8), # (1.0+1.0,2.0), # (-1+(-1),-2) # ]) # @pytest.mark.parametrize("data1,data2,expect",[ # (1,2,3), # (1.0,2.0,3.0), # (-1,-2,-3) # ]) @pytest.mark.parametrize("data1,data2,expect", yaml.safe_load(open("data/add.yml"))) def test_add(self, data1, data2, expect): result = self.calc.add(data1, data2) assert result == expect @pytest.mark.parametrize("data1,data2,expect", [(2, 1, 2), (2, 0, "integer division or modulo by zero"), (1, 2, 0.5)]) def test_div(self, data1, data2, expect): result = self.calc.div(data1, data2) assert result == expect
class calc: def setup(self): self.calc = Calc() def test_add(self): assert self.calc.add(1, 2) == 3 def test_div(self): assert self.calc.div(1, 2) == 0.5
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.parametrize('test_data', add_data.get_data("norm")) @pytest.mark.run(order=2) @allure.step("测试加法函数") def test_add_1(self,test_data): print(step1) if 'add' in step1: result = self.calc.add(test_data['a'], test_data['b']) print(test_data['a']) print(test_data['b']) print(result) assert test_data['res']-result<0.0001 @pytest.mark.parametrize('test_data', sub_data.get_data("norm")) @allure.step("测试减法函数") def test_sub_1(self, test_data): print(step1) if 'sub' in step1: result = self.calc.sub(test_data['a'], test_data['b']) print(test_data['a']) print(test_data['b']) print(result) assert test_data['res'] - result < 0.0001 @pytest.mark.parametrize('test_data', mul_data.get_data("norm")) @allure.step("测试乘法函数") def test_mul_1(self, test_data): print(step1) if 'mul' in step1: result = self.calc.mul(test_data['a'], test_data['b']) print(test_data['a']) print(test_data['b']) print(result) assert test_data['res'] - result < 0.0001 @pytest.mark.parametrize('test_data', div_data.get_data("norm")) @pytest.mark.run(order=1) @allure.step("测试除法函数") def test_div_1(self,test_data): print(step1) for step in step1: print(f"step ==== > {step}") if 'div' in step1: try: result = self.calc.div(test_data['a'], test_data['b']) print(test_data['a']) print(test_data['b']) print(result) except ZeroDivisionError: print("0不能做除数") assert test_data['res']-result<0.0001
class TestCalc: def test_add(self): self.calc = Calc() result = self.calc.add(1, 2) print(result) assert 3 == result def test_div(self): self.calc = Calc() result = self.calc.div(2, 2) assert 1 == result
class TestCalc: def setup_class(self): self.calc = Calc() @pytest.mark.parametrize('a,b,answer', [(1, 2, 3), (3, 4, 7)]) def test_add(self, a, b, answer): result = self.calc.add(a, b) assert result == answer @pytest.mark.parametrize('a,b,answer', [(4, 2, 2), [9, 3, 2]]) def test_div(self, a, b, answer): result = self.calc.div(a, b) assert result == answer
class TestCalc(): @pytest.fixture() def test_new_calc(self): self.calc_ob = Calc() @pytest.mark.parametrize("a,b,add", yaml.safe_load(open("datas/add.yml"))) def calc_add(self, test_new_calc, a, b, add): steps_ = steps() for step in steps_: if 'add' == step: result = self.calc_ob.add(a, b) assert result == add # assert self.calc_ob.add(a,b)==sum @pytest.mark.parametrize("a,b,sub", yaml.safe_load(open("datas/sub.yml"))) def calc_sub(self, test_new_calc, a, b, sub): steps_ = steps() for step in steps_: if 'sub' == step: result = self.calc_ob.sub(a, b) if isinstance(result, str): print(result) assert "Exception" in result else: assert '%.5f' % result == '%.5f' % sub # assert self.calc_ob.add(a,b)==sum @pytest.mark.parametrize("a,b,mul", yaml.safe_load(open("datas/mul.yml"))) def calc_mul(self, test_new_calc, a, b, mul): calc_result = self.calc_ob.mul(a, b) if isinstance(calc_result, str): print(calc_result) assert "Exception" in calc_result else: assert '%.5f' % calc_result == '%.5f' % mul # @pytest.mark.skip("div") @pytest.mark.parametrize("a,b,div", yaml.safe_load(open("datas/div.yml"))) def calc_div(self, test_new_calc, a, b, div): steps_ = steps() for step in steps_: if 'div' == step: calc_result = self.calc_ob.div(a, b) if isinstance(calc_result, str): print(calc_result) assert "Exception" in calc_result else: assert '%.5f' % calc_result == '%.5f' % div
class TestCalc: def setup(self): self.calc = Calc() def test_add(self): assert self.calc.add(1, 2) == 3 def test_div(self): assert self.calc.div(1, 2) == 0.5 def test_params(self): data = (1, 2) self.calc.add2(data) self.calc.add(*data)
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.run(order=2) # @pytest.mark.add @pytest.mark.parametrize('a, b, expected', yaml.safe_load(open("../data/calc_add.yaml"))) def calc_add(self, a, b, expected): try: actual_result = self.calc.add(a, b) assert round(actual_result, 2) == expected except TypeError: print("请输入数字") @pytest.mark.run(order=1) # @pytest.mark.sub @pytest.mark.parametrize('a, b, expected', yaml.safe_load(open("../data/calc_sub.yaml"))) def calc_sub(self, a, b, expected): try: actual_result = self.calc.sub(a, b) assert round(actual_result, 2) == expected except TypeError: print("请输入数字") @pytest.mark.run(order=3) # @pytest.mark.div @pytest.mark.parametrize('a, b, expected', yaml.safe_load(open("../data/calc_div.yaml"))) def calc_div(self, a, b, expected): try: if b == 0: print("除数不能为0") else: actual_result = self.calc.div(a, b) assert round(actual_result, 2) == expected except TypeError: print("请输入数字") @pytest.mark.run(order=4) # @pytest.mark.div @pytest.mark.parametrize('a, b, expected', yaml.safe_load(open("../data/calc_mul.yaml"))) def calc_div(self, a, b, expected): try: actual_result = self.calc.mul(a, b) assert round(actual_result, 2) == expected except TypeError: print("请输入数字")
class TestCalc: @pytest.mark.parametrize("a,b,expected", yaml.load(open(r'../data/adddata.yaml'))) def test_add(self, a, b, expected): self.calc = Calc() result = self.calc.add(a, b) print(result) assert expected == result @pytest.mark.parametrize("a,b,expected", yaml.load(open('../data/divdata.yaml'))) def test_div(self, a, b, expected): self.calc = Calc() result = self.calc.div(a, b) print(result) assert expected == result
class TestCalc: def setup(self): self.calc = Calc() def test_add(self): result = self.calc.add(3, 2) print(result) def test_add_1(self): result = self.calc.add(1, 2) print(result) def test_div(self): result = self.calc.div(2, 2) assert 1 == result # pytest.main(['-vs','test_pytest.py::test_add_1'])
class TestCal(): @pytest.fixture() def setup(self): self.calc = Calc() @pytest.mark.add @pytest.mark.parametrize('a,b', yaml.safe_load(open("../python/data.yaml"))) def calc_add_1(self, a, b, setup): result1 = self.calc.add(a, b) c = a + b assert result1 == c print(result1) @pytest.mark.parametrize('a,b', yaml.safe_load(open("../python/data.yaml"))) @pytest.mark.div def calc_div_1(self, a, b, setup): try: result1 = self.calc.div(a, b) except: return 'division by zero' assert result1 == 'division by zero' assert result1 == a / b print(result1) @pytest.mark.parametrize('a,b', yaml.safe_load(open("../python/data.yaml"))) @pytest.mark.mul def calc_multiply_1(self, a, b, setup): result1 = self.calc.multiply(a, b) assert result1 == a * b print(result1) @pytest.mark.parametrize('a,b', yaml.safe_load(open("../python/data.yaml"))) @pytest.mark.sub def calc_subtract_1(self, a, b, setup): result1 = self.calc.subtract(a, b) assert result1 == a - b print(result1)
class TestCalc: def setup(self): self.calc = Calc() ''' 1.正整数相加 2.一正一负相加(结果为正,负,0) 3.负数相加 4.小数相加 5.整数加小数(正负) 6.大数相加 7. 0和整数 (正负) 8. 都为0 ''' @pytest.mark.parametrize("a,b,r", add_data) def test_add_1(self, a, b, r): assert self.calc.add(a, b) == r ''' 1.正整数相除 2.负整数相除 3.分子或分母为小数 4.结果为正整数 5.结果为小数 6.结果负数 7.分子为0 8.分母为0 9.两者都为0 10. 大数 ''' @pytest.mark.parametrize(['a', 'b', 'r'], div_data) def test_div_1(self, a, b, r): if b == 0: with pytest.raises(ZeroDivisionError, match='除数不能为0'): raise ZeroDivisionError('除数不能为0') else: assert self.calc.div(a, b) == r
class TestCalc: def setup(self): self.calc = Calc() def test_cap(self): result = self.calc.add(2, 2) assert 4 == result # @pytest.mark.run(order=3) def test_add(self): result = self.calc.add(1, 2) assert 3 == result # @pytest.mark.run(order=2) def test_div(self): result = self.calc.div(2, 2) assert 1 == result # @pytest.mark.run(order=2) def test_cap_1(self): result = self.calc.add(2, 2) assert 4 == result
class TestCalc: def setup(self): self.calc = Calc() # @pytest.mark.demo # @pytest.mark.second def test_add(self): result = self.calc.add(1, 2) print(result) assert 3 == result @pytest.mark.parametrize('data1, data2, expect', yaml.safe_load(open('datas/add1.yml'))) def test_add_1(self, data1, data2, expect): steps1 = steps() for step in steps1: print(f"step ==== > {step}") if 'add' == step: result = self.calc.add(data1, data2) elif 'add1' == step: result = self.calc.add1(data1, data2) print(result) assert expect == result # result = self.calc.add(data1, data2) # print(result) # assert expect == result # # result1 = self.calc.add1(data1, data2) # print(result1) # assert expect == result1 # @pytest.mark.run(order=-1) def test_div(self): # self.calc = Calc() result = self.calc.div(2, 2) assert 1 == result
class TestCalc: def setup(self): self.calc=Calc() @pytest.mark.parametrize('a,b,c',yaml.safe_load(open("../datas/add.yml"))) def test_add(self, a, b, c): result = self.calc.add(a, b) assert c == result @pytest.mark.parametrize('a,b,c',[(0,0,0),(1,1,0),(-1,-1,0)]) def test_sub(self, a, b, c): result=self.calc.sub(a,b) assert c==result @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 1), (-1, -1, 1)]) def test_mul(self, a, b, c): result = self.calc.mul(a, b) assert c == result @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 1), (-1, -1, 1)]) def test_div(self, a, b, c): result = self.calc.div(a, b) assert c == result
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 2), (-1, -1, -2)]) def test_add(self, a, b, c): result = self.calc.add(a, b) assert c == result @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 0), (-1, -1, 0)]) def test_sub(self, a, b, c): result = self.calc.sub(a, b) assert c == result @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 1), (-1, -1, 1)]) def test_mul(self, a, b, c): result = self.calc.mul(a, b) assert c == result @pytest.mark.parametrize('a,b,c', [(0, 0, 0), (1, 1, 1), (-1, -1, 1)]) def test_div(self, a, b, c): result = self.calc.div(a, b) assert c == result
def test_div_6(self): calc = Calc() assert calc.div(10, 0) == 0
def test_div_5(self): calc = Calc() assert calc.div(-79, 3) == -26.33
def test_div_4(self): calc = Calc() assert calc.div(98, 3) == 32.67
def test_div_3(self): calc = Calc() assert calc.div(0, (-8)) == 0
def test_div_2(self): calc = Calc() assert calc.div(-80, (-5)) == 16