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(): @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() @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: 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
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_class(self): self.calc = Calc() print("初始化计算器...") # 加法,正数,0,负数,大数,小数,非法字符 data = [[1, 2, 3], [0, 0, 0], [-1, -2, -3], [0.1, 0.2, 0.3], [0.1, 1, 1.1], [0.1, -1, -0.9], [1, 'a', ''], [1, '$', ''], [1, '', ''], [9999999, 9999999, 19999998]] @pytest.mark.parametrize('a,b,expect', data) def test_add(self, a, b, expect): # self.calc = Calc() if isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.add(a, b) assert result == expect else: pytest.xfail(reason='type is error') # # # 除法 整数,除数为0,小数,负数,大数,非法字符 data1 = [[2, 1, 2], [1, 0, ''], [0.2, 0.1, 2], [-2, -1, 2], [9999999, 1, 9999999], [-2, 1, -2], [1, 'a', ''], ['$', 2, ''], ['', '', ''], [0, 1, 0]] @pytest.mark.parametrize('a,b,expect', data1) def test_div(self, a, b, expect, fix): # self.calc = Calc() if b == 0: pytest.xfail(reason='divisor can\'t be zero') elif isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.div(a, b) assert result == expect else: pytest.xfail(reason='type is error') # 乘法 正数,负数,小数,整数,0,大数,1,非法字符 data2 = [[1, 2, 2], [1, 1, 1], [1, -3, -3], [0, 1, 0], [0.1, 0.1, 0.01], [9999999, 9999999, 99999980000001], [-2, -2, 4], ['', 1, ''], ['a', 2, ''], ['$', 3, '']] @pytest.mark.parametrize('a,b,expect', data2) def test_mult(self, a, b, expect): # self.calc = Calc() if isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.mult(a, b) assert result == expect else: pytest.xfail(reason='type is error') # 减法 正数,负数,小数,整数,0,大数,非法字符 data3 = [[1, 1, 0], [0, 5, -5], [-5, 3, -8], [0.3, -0.1, 0.4], [-0.2, 0.1, -0.3], [9999999, 34, 9999965], [1, '', ''], ['a', 2, ''], ['$', 3, '']] @pytest.mark.parametrize('a,b,expect', data3) def test_sub(self, a, b, expect): # self.calc = Calc() if isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.sub(a, b) assert result == expect else: pytest.xfail(reason='type is error') def teardown_class(self): print("计算器...") # pytest.main(['-vs','test_pytest.py::TestCalc::test_div']) # if __name__ == '__main__': # pytest.main(['-vs','test_pytest.py::TestCalc::test_div'])