Exemplo n.º 1
0
class TestCalc:

    def setup_class(self):
        print("开始计算。。。")
        # 实例化计算器类
        self.calc = Calculator()

    def teardown_class(self):
        print("计算结束")

    @pytest.mark.parametrize(
        "a, b, expect", add_data, ids=myid  # 指定测试用例名称
    )
    def test_add(self, a, b, expect):
        # 实例化计算器类
        # calc = Calculator()
        # 调用加法方法
        result = self.calc.sum(a, b)
        # 判断结果类型是否为浮点型
        if isinstance(result, float):
            result = round(result, 2)
        # 进行断言
        assert result == expect

    @pytest.mark.parametrize('a, b, expect', sub_data, ids=sub_myid)
    def test_sub(self, a, b, expect):
        result = self.calc.sub(a, b)
        if isinstance(result, float):
            result = round(result, 2)
        assert result == expect
Exemplo n.º 2
0
class TestCalc:
    def setup_class(self):
        print("开始计算")
        self.calc = Calculator()

    def teardown_class(self):
        print("计算结束")

    @pytest.mark.parametrize("a, b, expect", add_datas, ids=myid)
    @pytest.mark.add
    def test_add(self, a, b, expect):
        result = self.calc.add(a, b)
        if isinstance(result, float):
            result = round(result, 2)
        # 得到相加结果之后写断言
        assert result == expect

    @pytest.mark.parametrize("a, b, expect1", mul_datas, ids=myid)
    @pytest.mark.mul
    def test_mul(self, a, b, expect1):
        result = self.calc.mul(a, b)
        if isinstance(result, float):
            result = round(result, 2)
        # 得到相加结果之后写断言
        assert result == expect1

    @pytest.mark.parametrize("a, b, expect1", sub_datas, ids=myid)
    @pytest.mark.sub
    def test_sub(self, a, b, expect1):
        result = self.calc.sub(a, b)
        if isinstance(result, float):
            result = round(result, 2)
        # 得到相加结果之后写断言
        assert result == expect1

    @pytest.mark.parametrize("a, b, expect1", div_datas, ids=myid)
    @pytest.mark.div
    def test_div(self, a, b, expect1):
        result = self.calc.div(a, b)
        if isinstance(result, float):
            result = round(result, 2)
        # 得到相加结果之后写断言
        assert result == expect1