class TestCalculator: def setup_class(self): print('开始计算') # 实例化变量 self.cale = Calculator() def teardown_class(self): print('计算结束') @pytest.mark.parametrize('a, b, expect', yaml.safe_load(open('datas.yml'))['add'], ids=yaml.safe_load(open('datas.yml'))['myid_add']) def test_add(self, a, b, expect): result = self.cale.add(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect @pytest.mark.parametrize('a, b, expect', yaml.safe_load(open('datas.yml'))['div'], ids=yaml.safe_load(open('datas.yml'))['myid_div']) def test_div(self, a, b, expect): if b != 0: result = self.cale.div(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect else: print('分母不能为0')
class TestCalc: def setup_class(self): self.calc = Calculator() def setup(self): print('计算开始') def teardown(self): print('计算结束') # 读取calc.yaml文件 with open('../datas/calc.yaml') as f: datas = yaml.safe_load(f)['add'] add_datas = datas['datas'] ids = datas['ids'] with open('../datas/calc.yaml') as f: div_datas = yaml.safe_load(f)['div'] div_data = div_datas['div_datas'] div_ids = div_datas['div_ids'] @pytest.mark.parametrize("c,d,expect", div_data, ids=div_ids) def test_div(self, c, d, expect): result = self.calc.div(c, d) assert result == expect @pytest.mark.parametrize("a,b,expect", add_datas, ids=ids) def test_add(self, a, b, expect): # 实例化计算器类 # calc = Calculator() result = self.calc.add(a, b) assert round(result, 2) == expect
class TestCalc: def setup_class(self): print('开始计算') # 实例化计算器类 self.calc = Calculator() def teardown_class(self): print('计算结束') @pytest.mark.parametrize('a, b, expect', datas['add']['datas'], ids=datas['add']['myid']) def test_add(self, a, b, expect): # 调用add方法 result = self.calc.add(a, b) # 判断如果结果为浮点数,保留两位小数 if isinstance(result, float): result = round(result, 2) # 得到结果后,设置断言 assert result == expect @pytest.mark.parametrize('a, b, expect', datas['div']['datas'], ids=datas['div']['myid']) def test_div(self, a, b, expect): # 调用div方法 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: 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: #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(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