pytest实现计算器---allure生成测试报告

it2025-01-17  3

#!/usr/bin/env python # -*- coding: utf-8 -*- import pytest import yaml from pythoncode.calculator import Calculator def get_datas(): with open ("data/data.ymal", encoding = "utf-8") as f: data = yaml.safe_load(f) return data class TestCalc: # def setup(self): # print("计算开始") # self.calc = Calculator() # # def teardown(self): # print("计算结束") @pytest.mark.run(order = 2) @pytest.mark.parametrize('a,b,expect', get_datas()['add']['data'], ids = get_datas()['add']['ids']) def test_add(self, get_calc,a, b, expect): result = get_calc.add(a, b) assert result == expect @pytest.mark.run(order = 1) @pytest.mark.parametrize('a,b,expect',get_datas()['div']['data'], ids = get_datas()['div']['ids']) def test_div(self,get_calc,a,b,expect): if b == 0: try: get_calc.div(a, b) except ZeroDivisionError as e: print("除数不能为0") else: result = get_calc.div(a, b) assert result == expect import pytest from pythoncode.calculator import Calculator @pytest.fixture(scope = "module") def get_calc(): calc = Calculator() print("计算开始") yield calc print("计算结束")
最新回复(0)