class TestCalc: def setup_class(self): print("开始计算") # 实例化计算器类 self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize( "a, b, expect", add_datas, ids=add_myid ) def test_add(self, a, b, expect): # 实例化计算器的类 # calc = Calculator() # 调用 add 方法 result = self.calc.add(a, b) # 判断result 是浮点数,保留2位小数 if isinstance(result, float): result = round(result, 2) # 断言 assert result == expect @pytest.mark.parametrize( "a, b, expect", div_datas, ids=div_myid ) def test_div(self, a, b, expect): result = self.calc.div(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect
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 ) def test_add(self, a, b, expect): # 实例化计算器类 # calc = Calculator() # 调用add方法 result = self.calc.add(a, b) if isinstance(result,float): result = round(result, 2) # 得到结果之后,需要写断言 assert result == expect print(f"{a}+{b}={expect}") @pytest.mark.parametrize( "a, b, expect", div_datas, ids=myid1 ) # 除法 def test_div(self, a, b, expect): result1 = self.calc.div(a, b) assert result1 == expect print(f"{a}/{b}={expect}")
class TestCalc: #setup方法,用例执行的前提条件 def setup_class(self): print("开始计算") #实例化计算器 #加了self.则局部变量变成实例变量,类中的其他方法也可用 self.calc = Calculator() # teardown方法,cleanup action def teardown_class(self): print("结束计算") @pytest.mark.add @pytest.mark.parametrize("a, b, expect", datas_add['datas'], ids=datas_add['myid']) def test_add(self, a, b, expect): result = self.calc.add(a, b) # isinstance()函数用来判断一个对象是否是一个已知的类型 if isinstance(result, float): result = round(result, 2) assert expect == result @pytest.mark.div @pytest.mark.parametrize("a, b, expect", datas_div['datas'], ids=datas_div['myid']) def test_div(self, a, b, expect): result = self.calc.div(a, b) # isinstance()函数用来判断一个对象是否是一个已知的类型 if isinstance(result, float): result = round(result, 2) assert expect == result
class TestCalc2: def setup_class(self): """ 实例化 计算器类,并且获取yaml文件数据准备后续的参数化 :return: """ print("开始计算") self.calc = Calculator() def teardown_class(self): print("结束计算") @pytest.mark.parametrize('a,b,expect', add_datas_True) def test_add_True(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,expect', add_datas_False) def test_add_Fasle(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,expect', div_datas_True) def test_div_True(self, a, b, expect): if b == 0: print("被除数不能为0") assert False else: result = self.calc.div(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect @pytest.mark.parametrize('a,b,expect', div_datas_False) def test_div_False(self, a, b, expect): if b == 0: print("被除数不能为0") assert False else: result = self.calc.div(a, b) if isinstance(result, float): result = round(result, 2) assert result != expect
class TestCalc: def setup_class(self): print("开始计算") # 实例化计算器的类,加self将局部变量改造成实例变量 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): # 实例化计算器的类 # calc = Calculator() # 调用add方法 result = self.calc.add(a, b) # 加浮点数判断 if isinstance(result, float): result = round(result, 2) # 得到结果之后,需要写断言 assert result == expect pass # def test_add2(self,a,b,expect): # # 实例化计算器的类 # # calc = Calculator() # # 调用add方法 # result = self.calc.add(0.5, 2.7) # # 得到结果之后,需要写断言 # assert result == 3.2 # pass # # def test_add3(self,a,b,expect): # # 实例化计算器的类 # # calc = Calculator() # # 调用add方法 # result = self.calc.add(-1.5, 8) # # 得到结果之后,需要写断言 # assert result == 6.5 # pass @pytest.mark.div def test_div(self): print("test_div") @pytest.mark.sub def test_sub(self): print("test_sub") @pytest.mark.mul def test_mul(self): print("test_mul")
class TestCalc: def setup(self): self.calc = Calculator() print("【开始计算】") def teardown(self): print("【结束计算】") # 参数化 # @pytest.mark.parametrize( # "a, b, expect", # [ # (1, 1, 2), # (0.1, 0.1, 0.2), # (-1, -1, -2), # (0.1, 0.2, 0.3) # ], ids=['int', 'float', 'negative', 'round'] # ) # 读取yaml文件内容进行参数化操作 @pytest.mark.parametrize( "a, b, expect", yaml.safe_load(open("../datas/calc.yaml"))["datas"]["add_datas"], ids=yaml.safe_load(open("../datas/calc.yaml"))["datas"]["myid"]) def test_add(self, a, b, expect): # 调用add方法 result = self.calc.add(a, b) # 判断 result是浮点数,做出保留2位小数的处理 if isinstance(result, float): result = round(result, 2) # 得到结果后,需要写断言 assert result == expect @pytest.mark.parametrize( "a, b, expect", yaml.safe_load(open("../datas/calc.yaml"))["datas"]["div_datas"], ids=yaml.safe_load(open("../datas/calc.yaml"))["datas"]["myid"]) def test_div(self, a, b, expect): # 调用div方法 if expect == "error": raise Exception("除数不能为0") else: result = self.calc.div(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect
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
class TestCalc: def setup_class(self): # 实例化计算器的类 self.calc = Calculator() print("开始计算") def teardown_class(self): print("计算结束") # @pytest.mark.parametrize():装饰器 # 以(key,valuse)的方式定义方法中的参数及参数值 # ( "a,b,expect", # [ # (1, 1, 2), # (2, 2, 4), # (3, 3, 6), # ], ids = ['1', '2', '3']) @pytest.mark.parametrize( "a,b,expect", add_datas,ids=myid ) #加法 def test_add(self, a, b, expect): result = self.calc.add(a, b) # 得到结果之后,需要写断言 # round(变量,2) 保留2位小数 # if isinstance(result,float) 判断如果结果是小数,将进入round方法中 if isinstance(result, float): result = round(result, 2) print(result) assert result, 2 == expect @pytest.mark.parametrize( "a,b,expect", div_datas,ids=div_myid ) #除法 def test_div(self,a,b,expect): result_div = self.calc.div(a,b) assert result_div == expect
class TestCalc1: def setup_class(self): print("开始计算") # 实例化计算器类 self.calc = Calculator() def teardown_class(self): print("计算结束") def setup(self): print("每个测试用例开始") def teardown(self): print("每个测试用例结束") # 加法测试用例 @pytest.mark.parametrize( "a,b,expect", add_datas, ids=add_myid ) def test_add1(self, a, b, expect): result = self.calc.add(a, b) # 判断result是浮点数,做出保留2位小数的处理 if isinstance(result, float): result = round(result, 2) # 得到结果之后,需要写断言 assert result == expect #除法测试用例 @pytest.mark.parametrize( "a,b,expect", div_datas, ids=div_myid ) def test_div1(self, a, b, expect): #添加异常处理,被除数不能为0 try: result = self.calc.div(a, b) # 得到结果之后,需要写断言 assert result == expect except ZeroDivisionError: print('被除数不能为0')
class TestCalc: def setup_class(self): print('开始计算') # 实例化计算器类 self.calc = Calculator() def teardown_class(self): print('计算结束') # 传入参数,python_code.calc替代 # @pytest.mark.parametrize( # "a, b, expect", # [ # (1, 1, 2), # (0.1, 0.1, 0.2), # (-1, -1, -2), # (0.1, 0.2, 0.3) # ], ids=['int', 'float', 'negative', 'round'] # ) @pytest.mark.parametrize("a, b, expect", add_datas, ids=add_myid) def test_add(self, a, b, expect): # 实例化计算器类 # calc = Calculator() # 调用add方法 result = self.calc.add(a, b) # 判断result是浮点数,作出保留2位小数的处理 if isinstance(result, float): result = round(result, 2) # 得到结果之后,需要写断言 assert result == expect @pytest.mark.parametrize("a, b, expect", div_datas, ids=div_myid) def test_div(self, a, b, expect): result = self.calc.div(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect
class TestCalc: def setup_class(self): # 实例化计算器的类 print("开始计算,实例化计算类") self.calc = Calculator() def teardown_class(self): print("计算结束!!!") @pytest.mark.parametrize( "a,b,expect", # [ # (1,1,2), # (0.1,0.1,0.2), # (-1,-1,-2), # (0.1,0.2,0.3) # ] # add_datas,ids=['int','float','negative','round']#给列表里的三个用例起名字 # add_datas,ids=['int','float','round','fail','negative'] add_datas, ids=myid) def test_add(self, a, b, expect): """ 实例化计算器的类 :return: """ # calc=Calculator() # 调用add方法 result = self.calc.add(a, b) # 判断result 是否是浮点数,做出保留小数点后2位的处理 if isinstance(result, float): result = round(result, 2) #避免浮点数计算 python底层转换二进制进行计算的错误,需要round函数进行四舍五入 # 得到结果之后,需要写断言 assert result == expect @pytest.mark.add() def test_add3(self): result = self.calc.add(0.1, 0.2) #判断result 是否是浮点数,做出保留小数点后2位的处理 if isinstance(result, float): result = round(result, 2) assert result == 0.3 #避免浮点数计算 python底层转换二进制进行计算的错误,需要round函数进行四舍五入 def test_add1(self): """ 实例化计算器的类 :return: """ # calc=Calculator() # 调用add方法 result = self.calc.add(0.1, 0.1) # 得到结果之后,需要写断言 assert result == 0.2 def test_add2(self): """ 实例化计算器的类 :return: """ # calc=Calculator() # 调用add方法 result = self.calc.add(-1, -1) # 得到结果之后,需要写断言 assert result == -2 @pytest.mark.sub def test_sub(self): print("test_sub") @pytest.mark.div def test_div(self): print("test_div") @pytest.mark.mul def test_mul(self): print("test_mul")
class TestCalc: def setup_class(self): # 实例化计算器 print("开始计算") self.calc = Calculator() def teardown_class(self): print("结束计算") # 参数化,a,b是参数,expect是预期结果 # ids是给用来命名 # @pytest.mark.parametrize( # "a,b,expect", # [ # (1,1,2), # (0.1,0.1,0.2), # (-1,-1,-2), # (0.1,0.2,0.3) # ],ids=['int','float','negative','round'] # ) # yaml方式 @pytest.mark.parametrize("a,b,expect", add_datas, ids=myid) def test_add4(self, a, b, expect): result = self.calc.add(a, b) # 判断是否是浮点数 if isinstance(result, float): result = round(result, 2) assert result == expect # #由于float会丢位,所以用round函数,保留两位 # def test_add5(self): # result = self.calc.add(0.1,0.2) # assert round(result,2)==0.3 # def test_add(self): # # #实例化计算器 # # calc = Calculator() # #调用add方法 # result = self.calc.add(1,1) # # #得到结果后,需要写短言 # assert result==2 # # def test_add1(self): # #实例化计算器 # # calc = Calculator() # #调用add方法 # result = self.calc.add(0.1,0.1) # # #得到结果后,需要写短言 # assert result==0.2 # # def test_add2(self): # #实例化计算器 # # calc = Calculator() # #调用add方法 # result = self.calc.add(-1,-1) # # #得到结果后,需要写短言 # assert result==-2 @pytest.mark.parametrize('a', [1, 2, 3]) @pytest.mark.parametrize('b', [4, 5, 6]) def test_param(self, a, b): print(f"a={a},b={b}")