class TestCalc: def setup_method(self): self.cal = Calculator() print("setup_method") @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5)]) @pytest.mark.add def test_add(self, a, b, result, open): #cal = Calculator() assert self.cal.add(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.div def test_div(self, a, b, result, open): cal = Calculator() assert self.cal.div(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.subtraction def test_subtraction(self, a, b, result, open): #cal = Calculator() assert self.cal.subtraction(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.multiplication def test_multiplication(self, a, b, result, open): assert self.cal.multiplication(a, b) == result
class TestCalc: # 类里面执行 def setup_class(self): print("类级别setup") self.calc = Calculator() def teardown_class(self): print("类级别teardown") @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['add']) def test_add(self, list1, calc): add_result = self.calc.add(list1[0], list1[1]) print(f'实际结果为:{add_result}, 类型为{type(add_result)}') assert Decimal(list1[2]).quantize(Decimal('0.00')) == add_result @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['div']) def test_div(self, list1, calc): assert Decimal(list1[2]).quantize(Decimal('0.00')) == self.calc.div( list1[0], list1[1]) @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['decrease']) def test_decrease(self, list1, calc): assert Decimal(list1[2]).quantize( Decimal('0.00')) == self.calc.decrease(list1[0], list1[1]) @pytest.mark.parametrize('list1', yaml.safe_load( open('./../calc.yml'))['multiplication']) def test_multiplication(self, list1, calc): print(list1) assert Decimal(list1[2]).quantize( Decimal('0.00')) == self.calc.multiplication(list1[0], list1[1])