测试人社区

pytest第二节课程,try的时候加入错误申诉局报错

######################test_calc_new.py##########################

class TestCalc:

    def test_add(self,get_calc,get_datas):
        result = None
        try:
            result = get_calc.add(get_datas[0],get_datas[1])
            #print(result)
            if isinstance(result,float):
                result = round(result,2)
        except Exception as e:
            print(e)

        assert result == get_datas[2]


########################conftest.py#########################

yaml_file_path = os.path.dirname(__file__) + "/cal.yml"

with open(yaml_file_path,'rb') as f:
    datas = yaml.safe_load(f)
    print(datas)

    add_datas = datas["add_datas"]
    add_ids = datas["add_ids"]

@pytest.fixture(params=add_datas, ids=add_ids)
def get_datas(request):
    print("开始计算")
    data = request.param
    print(f"request.param是:{data}")
    yield data
    print("结束计算")



#####################cal.yml里的########################
add_datas:
  - [3,5,8]
  - [-1,-2,-3]
  - [10000,10000,20000]
  - [0.1,0.2,0.3]
  - ["", 5, "请输入数字"]
add_ids:
  - "int"
  - "minus"
  - "bigint"
  - "float"
  - "exceip"
########################### calculator.py####################

class Calculator:
    def add(self,a,b):
        return a+b
    def sub(self,a,b):
        return a-b
    def mul(self,a,b):
        return a*b
    def div(self,a,b):
        return a/b

首先代码要注意格式化,参考答疑区置顶帖代码如何格式化,不格式化代码让阅读的人看起来很痛苦

霍格沃兹答疑区如何提问【提问前必看】

你的问题是什么,期望结果是什么,实际结果是什么,请补充一下哦