class TestCalc: datas: list = get_datas() # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.run(order=4) @pytest.mark.parametrize('a,b,expect', get_datas()['add']['datas'], ids=get_datas()['add']['ids']) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert expect == result @pytest.mark.run(order=3) @pytest.mark.parametrize('a,b,expect', get_datas()['subtract']['datas'], ids=get_datas()['subtract']['ids']) def test_subtract(self, a, b, expect): result = self.calc.subtract(a, b) assert expect == result @pytest.mark.run(order=2) @pytest.mark.parametrize('a,b,expect', get_datas()['multiply']['datas'], ids=get_datas()['multiply']['ids']) def test_multiply(self, a, b, expect): result = self.calc.multiply(a, b) assert expect == result @pytest.mark.run(order=1) @pytest.mark.parametrize('a,b,expect', get_datas()['div']['datas'], ids=get_datas()['div']['ids']) def test_div(self, a, b, expect): if b == 0: try: self.calc.div(a, b) except ZeroDivisionError as e: print("除数不能为0") else: result = self.calc.div(a, b) assert result == expect
class TestCaculator: def setup(self): self.cal = Calculator() print("start the calculation ---") @allure.feature("相加功能") @pytest.mark.search @pytest.mark.parametrize("a, b, result", get_data('add')[0], ids=get_data('add')[1]) def test_add(self, a, b, result): res = self.cal.add(a, b) res = round(res, 2) # 需要进行精度控制,比如 0.1+0.2会算出来0.30000000000000004,直接进行比较会出现不匹配的问题 assert result == res @allure.feature("减法") def test_substract(self): assert 1 == self.cal.subtract(2, 1) @allure.feature("乘法") def test_multiply(self): assert 2 == self.cal.multiply(1, 2) @allure.feature("除法") @pytest.mark.parametrize("a, b, result", get_data('divide')[0], ids=get_data('divide')[1]) def test_divide(self, a, b, result): try: res = self.cal.divide(a, b) res = round(res, 2) assert result == res print(result) except Exception as e: print(e) print("You can't divide by zero") # if b == 0: # try: # self.cal.divide(a, b) # except ZeroDivisionError as e: # print("You can't divide by zero") # else: # res = self.cal.divide(a, b) # res = round(res, 2) # assert result == res # print(result) def teardown(self): print("calculation completed ----")