class TestCalc: datas: list = get_datas() # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a, b, result", datas[0], ids=datas[1]) def test_add(self, a, b, result): print(f"a={a} , b ={b} ,result={result}") assert result == self.calc.add(a, b) def test_add1(self): datas = [[1, 1, 2], [100, 400, 300], [1, 0, 1]] for data in datas: print(data) assert data[2] == self.calc.add(data[0], data[1]) # TODO: 完善相加功能 # TODO: 相除功能 def test_div(self): pass
class TestCalc: def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize('a,b,expect', [ [1, 1, 2], [100, 1, 200], [0.1, 0.1, 0.2], [-1, -1, -2], [1, 0, 1], [0.1, 0.2, 0.3] ], ids=['int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case', 'smallnum_case']) def test_add(self, a, b, expect): # calc = Calculator() result = self.calc.add(a, b) assert result == expect @pytest.mark.parametrize('a,b,expect', [ [0.1, 0.1, 0.2], [0.1, 0.2, 0.3] ]) def test_add_float(self, a, b, expect): result = self.calc.add(a, b) assert round(result, 2) == expect @pytest.mark.parametrize('a,b,expect', [ [0.1, 0], [10, 0] ]) def test_div(self, a, b): with pytest.raises(ZeroDivisionError): self.calc.div(a, b)
class TestCalc: """调用get_datas()函数获取数据""" # datas:list = get_datas_function.get_datas() add_int_datas: list = get_datas_function.get_datas("add", "int") add_float_datas: list = get_datas_function.get_datas("add", "float") div_date_datas: list = get_datas_function.get_datas("div", "int") div_zero_datas: list = get_datas_function.get_datas("div", "div_by_zero") # print(add_int_datas) # print(float_int_datas) # 定义类开始要做的操作:实例化一个计算器 def setup_class(self): print("开始计算") self.cal = Calculator() def teardown_class(self): print("结束计算") # 给加法计算器添加一个search标签 @pytest.mark.search # 参数化,可以实现生成多条测试用例,即便有测试用例失败了,其他测试用例也可以正常执行,而不会中断执行。 @pytest.mark.parametrize("a,b,result", add_int_datas[0], ids=add_int_datas[1]) # 测试加法计算器 def test_add(self, a, b, result): print(f"a = {a},b = {b},result = {result}") # cal = Calculator() assert result == self.cal.add(a, b) @pytest.mark.parametrize("a,b,result", add_float_datas[0], ids=add_float_datas[1]) def test_float_add(self, a, b, result): print(f"a = {a},b = {b},result = {result}") # cal = Calculator() assert result == self.cal.add(a, b) # 测试减法计算器 # 给除法计算器添加一个标签 @pytest.mark.login @pytest.mark.parametrize("a,b,result", div_date_datas[0], ids=div_date_datas[1]) def test_div(self, a, b, result): print(f"a = {a},b = {b},result = {result}") # cal = Calculator() assert result == self.cal.div(a, b) @pytest.mark.parametrize("a,b,result", div_zero_datas[0], ids=div_zero_datas[1]) def test_div_zero(self, a, b, result): print(f"a = {a},b = {b},result = {result}") # cal = Calculator() with pytest.raises(ZeroDivisionError): print(self.cal.div(a, b))
class TestCalc(): add_int_data = get_datas('add','int') add_decimal_data = get_datas('add','decimal') div_int_data = get_datas('div','int_normal') div_zero_data= get_datas('div','int_error') div_combination = get_datas('div','int_normal_and_error') #前置条件 def setup_class(self): print("开始计算") self.calc=Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.login #和pytest.ini结合使用 @pytest.mark.parametrize("a,b,result",add_int_data[0],ids=add_int_data[1]) def test_add(self,a,b,result): print(f"a={a},b={b},result={result}") assert result == self.calc.add(a,b) @pytest.mark.search @pytest.mark.parametrize("a,b,result",add_decimal_data[0],ids=add_decimal_data[1]) def test_add_decimal(self,a,b,result): print(f"a={a},b={b},result={result}") assert round(result,2)==round(self.calc.add(a,b),2) @pytest.mark.parametrize("a,b,result", div_int_data[0], ids=div_int_data[1]) def test_div(self, a, b, result): print(f"a={a},b={b},result={result}") assert result == self.calc.div(a, b) @pytest.mark.parametrize("a,b,result", div_zero_data[0], ids=div_zero_data[1]) def test_div_zero(self, a, b, result): with pytest.raises(ZeroDivisionError): result = a/b #遇到分母为0时的2种处理方法,最好让用例尽量简单,不要混合写在一起 @pytest.mark.parametrize("a,b,result", div_combination[0], ids=div_combination[1]) def test_div_normal_and_zero(self, a, b, result): if b==0: with pytest.raises(ZeroDivisionError) as excinfo: self.calc.div(a,b) #断言异常类型type assert excinfo.type==ZeroDivisionError #断言异常类型value值 assert "division by zero" in str(excinfo.value) else: assert result==self.calc.div(a,b)
class TestCalc: data = deal_datas.get_datas # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,result", data('add', 'int')[0], ids=data('add', 'int')[1]) def test_add(self, a, b, result): assert result == self.calc.add(a, b) @pytest.mark.parametrize("a,b,result", data('add', 'float')[0], ids=data('add', 'float')[1]) def test_add_float(self, a, b, result): assert result == self.calc.add_float(a, b) @pytest.mark.parametrize("a,b,result", data('div', 'int')[0], ids=data('div', 'int')[1]) def test_div(self, a, b, result): if b == 0: try: self.calc.div(self, a, b) except Exception as f: print('除数不能为零') else: assert result == self.calc.div(a, b)
class TestCalc: datas: list = get_datas() # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a, b, expected", get_datas()['add']['datas'], ids=get_datas()['add']['ids']) def test_add(self, a, b, expected): result = self.calc.add(a, b) assert expected == result @pytest.mark.parametrize("a, b, expected", get_datas()['div']['datas'], ids=get_datas()['div']['ids']) def test_div(self, a, b, expected): if b == 0: try: self.calc.div(a, b) except ZeroDivisionError as e: print("Divisor should not be zero") else: result = self.calc.div(a, b) assert expected == result
class TestCalc: # 定义类开始要做的操作:实例化一个计算器 def setup_class(self): print("开始计算") self.cal = Calculator() def teardown_class(self): print("结束计算") # 测试加法计算器 # 给加法计算器添加一个标签 @pytest.mark.search # 参数化,可以实现生成多条测试用例,即便有测试用例失败了,其他测试用例也可以正常执行,而不会中断执行。 @pytest.mark.parametrize( "a,b,result", [[1, 1, 2], [100, 200, 300], [3, 4, 8], [200, 200, 400]]) #获取yaml文件的数值 def get_datas(self): with open("./datas/calc.yml") as f: self.datas = yaml.safe_load(f) print("datas") return self.datas def test_add(self, a, b, result): print(f"a = {a},b = {b},result = {result}") # cal = Calculator() assert result == self.cal.add(a, b) # 测试减法计算器 # 给除法计算器添加一个标签 @pytest.mark.login def test_div(self): # cal = Calculator() assert 2 == self.cal.div(4, 2)
class TestCalc: datas: list = get_datas() # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.run(order=4) @pytest.mark.parametrize('a,b,expect', get_datas()['add']['datas'], ids=get_datas()['add']['ids']) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert expect == result @pytest.mark.run(order=3) @pytest.mark.parametrize('a,b,expect', get_datas()['subtract']['datas'], ids=get_datas()['subtract']['ids']) def test_subtract(self, a, b, expect): result = self.calc.subtract(a, b) assert expect == result @pytest.mark.run(order=2) @pytest.mark.parametrize('a,b,expect', get_datas()['multiply']['datas'], ids=get_datas()['multiply']['ids']) def test_multiply(self, a, b, expect): result = self.calc.multiply(a, b) assert expect == result @pytest.mark.run(order=1) @pytest.mark.parametrize('a,b,expect', get_datas()['div']['datas'], ids=get_datas()['div']['ids']) def test_div(self, a, b, expect): if b == 0: try: self.calc.div(a, b) except ZeroDivisionError as e: print("除数不能为0") else: result = self.calc.div(a, b) assert result == expect
class TestCaculator: def setup(self): self.cal = Calculator() print("start the calculation ---") @allure.feature("相加功能") @pytest.mark.search @pytest.mark.parametrize("a, b, result", get_data('add')[0], ids=get_data('add')[1]) def test_add(self, a, b, result): res = self.cal.add(a, b) res = round(res, 2) # 需要进行精度控制,比如 0.1+0.2会算出来0.30000000000000004,直接进行比较会出现不匹配的问题 assert result == res @allure.feature("减法") def test_substract(self): assert 1 == self.cal.subtract(2, 1) @allure.feature("乘法") def test_multiply(self): assert 2 == self.cal.multiply(1, 2) @allure.feature("除法") @pytest.mark.parametrize("a, b, result", get_data('divide')[0], ids=get_data('divide')[1]) def test_divide(self, a, b, result): try: res = self.cal.divide(a, b) res = round(res, 2) assert result == res print(result) except Exception as e: print(e) print("You can't divide by zero") # if b == 0: # try: # self.cal.divide(a, b) # except ZeroDivisionError as e: # print("You can't divide by zero") # else: # res = self.cal.divide(a, b) # res = round(res, 2) # assert result == res # print(result) def teardown(self): print("calculation completed ----")
class Test_Cala: def setup_class(self): print("验证开始啦!!!") def teardown_class(self): print("验证结束啦!!!") def setup(self): print("开始计算") self.cal = Calculator() def teardown(self): print("计算结束") @pytest.mark.parametrize(["a", "b", "expect"], get_data()[0], ids=get_data()[1]) def test_add(self, a, b, expect): add = self.cal.add(a, b) print(add) assert add == expect @pytest.mark.parametrize(["a", "b", "expect"], get_data()[2], ids=get_data()[3]) def test_sub(self, a, b, expect): sub = self.cal.sub(a, b) print(sub) assert sub == expect @pytest.mark.parametrize(["a", "b", "expect"], get_data()[4], ids=get_data()[5]) def test_mul(self, a, b, expect): mul = self.cal.mul(a, b) print(mul) assert mul == expect @pytest.mark.parametrize(["a", "b", "expect"], get_data()[6], ids=get_data()[7]) def test_div(self, a, b, expect): div = self.cal.div(a, b) print(div) assert div == expect
class TestCalc(): datas: list=get_datas() #前置条件 def setup_class(self): print("开始计算") self.calc=Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,result",datas[0],ids=datas[1]) def test_add(self,a,b,result): print(f"a={a},b={b},result={result}") assert result==self.calc.add(a,b) @pytest.mark.parametrize("a,b,result", datas[2], ids=datas[3]) def test_div(self, a, b, result): print(f"a={a},b={b},result={result}") assert result == self.calc.div(a, b)
class TestCalc: # 可以将初始化放到这里来,然后测试用例使用 def setup_class(self): self.calc = Calculator() def setup(self): print("开始计算") def teardown(self): print("计算结束") @pytest.mark.parametrize("a,b,result", yaml.safe_load(open("../data/calculator_add.yaml"))) def test_add(self, a, b, result): """ 加法测试用例 """ assert self.calc.add(a, b) == result pass # @pytest.mark.login @pytest.mark.parametrize("a,b,result", yaml.safe_load(open("../data/calculator_div.yaml"))) def test_div(self, a, b, result): """ 加法测试用例,添加了异常逻辑判断,除数为0捕获异常则通过 """ if b == 0: try: assert self.calc.div(a, b) == result print("除 0 异常数据未正常异常抛出") raise Exception("除 0 异常数据未正常异常抛出") except ZeroDivisionError as e: print("除 0 异常数据异常抛出") else: assert self.calc.div(a, b) == result pass
class TestCalc: add_datas: list = get_datas("add") div_datas: list = get_datas("div") # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a, b, result", add_datas[0], ids=add_datas[1]) def test_add(self, a, b, result): print(f"a={a} , b ={b} ,result={result}") assert result == self.calc.add(a, b) # def test_add1(self): # datas = [[1, 1, 2], [100, 400, 300], [1, 0, 1]] # for data in datas: # print(data) # assert data[2] == self.calc.add(data[0], data[1]) # 相除功能 @pytest.mark.parametrize("a, b, result", div_datas[0], ids=div_datas[1]) def test_div(self, a, b, result): if b == 0: print("除数不能为0") assert result == self.calc.div(a, b) elif a or b is not int: print("请输入数字") assert result == self.calc.div(a, b) else: print(f"a={a} , b ={b} ,result={result}") assert result == self.calc.div(a, b)
class TestCalc: datas: list = get_datas() def setup_class(self): print("开始计算") self.calc = Calculator() def teardown_class(self): print("结束计算") #相加功能 @pytest.mark.parametrize("a,b, result", datas[0], ids=datas[1]) def test_add(self, a, b, result): assert result == self.calc.add(a, b) # "a,b,result", datas[2] #相除功能 @pytest.mark.parametrize("a,b, result", datas[2], ids=datas[3]) def test_div(self, a, b, result): if b != 0: assert result == self.calc.div(a, b) elif b == 0: with pytest.raises(ZeroDivisionError): self.calc.div(a, b)
class TestCalc: datas1: list = get_datas1() datas2: list = get_datas2() # 前置条件 def setup_class(self): print("开始计算") self.calc = Calculator() # 后置条件 def teardown_class(self): print("结束计算") #测试相加功能 @pytest.mark.parametrize("a, b, result", datas1[0], ids=datas1[1]) def test_add(self, a, b, result): print(f"a={a} , b ={b} ,result={result}") assert result == self.calc.add(a, b) #测试相除功能 @pytest.mark.parametrize("a, b, result", datas2[0], ids=datas2[1]) def test_div(self, a, b, result): print(f"a={a} , b={b}, result={result}") assert result == self.calc.div(a, b)