Example #1
0
class TestCalc:
    def setup_class(self):
        print("setup_class")
        self.calc = Calc()

    def teardown_class(self):
        print("teardown_class")

    def setup(self):
        print("setup")

    def teardown(self):
        print("teardown")

    @pytest.mark.parametrize('a, b, expect',
                             yaml.safe_load(open("datas/data.yml")))
    # @pytest.mark.run(order=-1)
    def test_zadd_1(self, a, b, expect):
        result = self.calc.add(a, b)
        print(f"result = {result}")
        assert result == expect

    def get_steps(self):
        with open('steps/steps.yml') as f:
            return yaml.safe_load(f)

    def any_steps(self, data, expect):
        steps = self.get_steps()
        for step in steps:
            print(f"step == {step}")
            if 'add' == step:
                assert self.calc.add(*data) == expect
            elif 'add1' == step:
                assert self.calc.add1(data) == expect

    @pytest.mark.parametrize('a, b, expect',
                             yaml.safe_load(open("datas/data.yml")))
    # @pytest.mark.run(order=-1)
    def test_zadd_2(self, a, b, expect):
        data = (a, b)
        self.any_steps(data, expect)
        # result1 = self.calc.add(*data)
        # assert result1 == expect
        #
        # result2 = self.calc.add1(data)
        # assert result2 == expect

    # @pytest.mark.first
    def test_sub(self):
        assert 1 == self.calc.sub(2, 1)

    # @pytest.mark.run(order=3)
    def test_mul(self):
        assert 6 == self.calc.mul(2, 3)

    # @pytest.mark.run(order=4)
    def test_div_1(self):
        result = self.calc.div(1, 1)
        print(f"result = {result}")
        assert result == 1
class TestCalc:
    def setup(self):
        self.calc = Calc()

    @pytest.mark.parametrize(["a", "b", "c"],
                             yaml.safe_load(
                                 open(
                                     os.path.join(os.path.dirname(__file__),
                                                  '..', 'test_data',
                                                  'test_add_data.yaml'))))
    def test_add(self, a, b, c):
        print(f"测试数据分别是:{a},{b}")
        result = self.calc.add(a, b)
        print(result)
        assert c == result

    @pytest.mark.parametrize(["a", "b", "c"],
                             yaml.safe_load(
                                 open(
                                     os.path.join(os.path.dirname(__file__),
                                                  '..', 'test_data',
                                                  'test_div_data.yaml'))))
    def test_div_01(self, a, b, c):
        """正常情况下测试"""
        print(f"测试数据分别是:{a},{b}")
        result = self.calc.div(a, b)
        print(result)
        assert c == result

    @pytest.mark.parametrize(["a", "b"], [(5, 0)])
    def test_div_02(self, a, b):
        """除数为零"""
        print(f"测试数据分别是:{a}, {b}")
        with pytest.raises(ZeroDivisionError):
            self.calc.div(5, 0)
        # print(self.calc.div(5, 0))

    @pytest.mark.parametrize(["a", "b"], [(10, 3)])
    def test_div_03(self, a, b):
        """商为无限循环小数时"""
        print(f"测试数据分别是:{a}, {b}")
        result = self.calc.div(a, b)
        assert 3.3333333333 == round(result, 10)
Example #3
0
 def test_div(self):
     calc = Calc()
     result = calc.div(1, 1)
     assert result == 1