def setup_class(self): """ 实例化 计算器类,并且获取yaml文件数据准备后续的参数化 :return: """ print("开始计算") self.calc = Calculator()
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: #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 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: def setup_class(self): print("开始计算。。。") # 实例化计算器类 self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize( "a, b, expect", add_data, ids=myid # 指定测试用例名称 ) def test_add(self, a, b, expect): # 实例化计算器类 # calc = Calculator() # 调用加法方法 result = self.calc.sum(a, b) # 判断结果类型是否为浮点型 if isinstance(result, float): result = round(result, 2) # 进行断言 assert result == expect @pytest.mark.parametrize('a, b, expect', sub_data, ids=sub_myid) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) if isinstance(result, float): result = round(result, 2) assert result == expect
class TestCals: # 实例化计算器 def setup_class(self): self.calc = Calculator() @pytest.mark.run(order=1) # 参数化测试案例数据 @pytest.mark.parametrize("a,b,c", data, ids=myid) def test_add(self, a, b, c, before_cases): result = self.calc.addition(a, b) # 如果结果是浮点数,则保留两位小数 if isinstance(result, float): result = round(result, 2) # 添加断言 assert result == c # 除法测试用例 @pytest.mark.run(order=4) @pytest.mark.parametrize('a,b,c', [(1, 1, 1), (0.1, 0.1, 1), (-1, -0.1, 10), (1, 0, 0)]) def test_division(self, a, b, c, before_cases): # 异常处理 try: result = self.calc.division(a, b) # 添加断言 assert result == c except ZeroDivisionError: print('被除数不能为零') # 减法测试用例 @pytest.mark.run(order=2) @pytest.mark.usefixtures('before_cases') @pytest.mark.parametrize('a,b,c', [(1, 1, 0), (0.1, 0.1, 0), (-1, -0.1, -0.9)]) def test_subtraction(self, a, b, c): result = self.calc.subtraction(a, b) # 添加断言 assert result == c # 乘法测试用例 @pytest.mark.run(order=3) @pytest.mark.parametrize('a,b,c', [(1, 1, 1), (0.1, 0.1, 0.01), (-1, -0.1, 0.1), (1, 0, 0)]) def test_multiplication(self, a, b, c, before_cases): result = self.calc.multiplication(a, b) if isinstance(result, float): result = round(result, 2) # 添加断言 assert result == c
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): # 实例化计算器类 # calc = Calculator() # 定义一个变量接收add方法的返回值 # 调用相加方法 result = self.calc.add(a, b) if isinstance(result, float): result = round(result, 2) # 得到相加结果之后写断言 assert result == expect @pytest.mark.add def test_add1(self): # calc = Calculator() result = self.calc.add(0.1, 0.1) assert result == 0.2 @pytest.mark.add def test_add2(self): # calc = Calculator() result = self.calc.add(-1, -1) assert result == -2 # def test_add3(self): # result = self.add(0.1, 0.2) # assert round(result,2) == 0.3 @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")
def get_calc(): """ 实例化Calculator :return: calc """ calc = Calculator() return calc
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将局部变量改造成实例变量 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 TestCals: def setup_class(self): self.calc = Calculator() print('开始计算') def teardown_class(self): print('计算结束') def setup(self): print('执行案例开始') def teardown(self): print('执行案例结束') # 参数化测试案例数据 @pytest.mark.parametrize("a,b,c", data, ids=myid) def test_add(self, a, b, c): result = self.calc.addition(a, b) # 如果结果是浮点数,则保留两位小数 if isinstance(result, float): result = round(result, 2) # 添加断言 assert result == c # 除法测试用例 @pytest.mark.parametrize('a,b,c', [(1, 1, 1), (0.1, 0.1, 1), (-1, -0.1, 10), (1, 0, 0)]) def test_division(self, a, b, c): # 异常处理 try: result = self.calc.division(a, b) # 添加断言 assert result == c except ZeroDivisionError: print('被除数不能为零')
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
def setup_class(self): self.calc = Calculator() print('开始计算')
def setup_class(self): # 实例化计算器的类 self.calc = Calculator() print("开始计算")
def get_calc(): print('获取计算器实例') calc = Calculator() return calc
def get_calc(): print("开始计算") calc = Calculator() return calc
def get_cale(): print('实例化计算器') cale = Calculator() return cale
def setup_class(self): self.calc = Calculator()
def get_calc(): print("\n获取计算器实例") calc = Calculator() return calc
def get_calc(): print("实例化变量") calc = Calculator() return calc
def setup_class(self): # 实例化计算器的类 print("开始计算,实例化计算类") self.calc = Calculator()
def setup_class(self): print("开始计算。。。") # 实例化计算器类 self.calc = Calculator()
def setup_class(self): print("开始计算") # 实例化计算器的类,加self将局部变量改造成实例变量 self.calc = Calculator()