class TestCalc(): def setup_class(self): print("开始计算") # 实例化计算器 self.calc = Calculator() def teardown_class(self): print("结束计算") @pytest.mark.add def test_fun(self): # # 实例化计算器 # calc = Calculator() # 调用它的相加方法 result = self.calc.add(1, 1) # 断言 assert 2 == result @pytest.mark.add @pytest.mark.parametrize("a,b,expect", adddatas, ids=myid) def test_add(self, a, b, expect): # # 实例化计算器 # calc = Calculator() # 调用它的相加方法 result = self.calc.add(a, b) # 如果结果是浮点数,四舍五入取两位 if isinstance(result, float): result = round(result, 2) # 断言 assert expect == result @pytest.mark.add def test_add1(self): # # 实例化计算器 # calc = Calculator() # 调用它的相加方法 result = self.calc.add(100, 100) # 断言 assert 200 == result @pytest.mark.add def test_add2(self): # # 实例化计算器 # calc = Calculator() # 调用它的相加方法 result = self.calc.add(-1, -1) # 断言 assert -2 == result @pytest.mark.div def test_div(self): # calc = Calculator() result = self.calc.div(1, 1) assert 1 == result @pytest.mark.div def test_div1(self): # calc = Calculator() result = self.calc.div(-1, 1) assert -1 == result
class TestCalc: # setup_class, teardown_class每个类里面 执行前后分别 执行 def setup_class(self): self.cal = Calculator() print("类级别 setup") def teardown_class(self): print("类级别 teardown") # 函数级别 每条类里面的测试用例前和后分别 执行setup teardown def setup(self): print("setup") def teardown(self): print("teardown") @pytest.mark.parametrize('a, b, result', [ (1, 1, 3), (2, 2, 4), (100, 100, 200), (0.1, 0.1, 0.2), (-1, -1, -2) ] , ids=['int', 'int', 'bignum', 'float', 'fushu']) # @pytest.mark.add def test_add(self, a, b, result): # cal = Calculator() assert result == self.cal.add(a, b) assert result == self.cal.add(a, b) assert result == self.cal.add(a, b) # @pytest.mark.add def test_add1(self): # cal = Calculator() assert 3 == self.cal.add(1, 2) # @pytest.mark.div def test_div(self): # cal = Calculator() print("登录操作111") assert 1 == self.cal.div(2, 1) print("登录操作222") assert 1 == self.cal.div(2, 1) print("登录操作333") assert 1 == self.cal.div(2, 1) def test_assume(self): print("登录操作") pytest.assume(1 == 2) print("搜索操作") pytest.assume(2 == 2) print("加购操作") pytest.assume(3 == 2)
class TestCalc: # setup_class, teardown_class每个类里面 执行前后分别 执行 def setup_class(self): self.cal = Calculator() print("类级别 setup") def teardown_class(self): print("类级别 teardown") # 函数级别 每条类里面的测试用例前和后分别 执行setup teardown def setup(self): print("setup") def teardown(self): print("teardown") @pytest.mark.parametrize('a, b, result', adddatas, ids=addids) # @pytest.mark.add def test_add(self, a, b, result): steps(a, b, result) # cal = Calculator() # assert result == self.cal.add(a, b) # assert result == self.cal.add1(a, b) # assert result == self.cal.add2(a, b) # @pytest.mark.add def test_add1(self): # cal = Calculator() assert 3 == self.cal.add(1, 2) # @pytest.mark.div @pytest.mark.parametrize('a,b,c', divdatas, ids=divids) def test_div(self, a, b, c): # cal = Calculator() print("登录操作111") assert c == self.cal.div(a, b) print("登录操作222") assert 1 == self.cal.div(2, 1) print("登录操作333") assert 1 == self.cal.div(2, 1) def test_assume(self): print("登录操作") pytest.assume(1 == 2) print("搜索操作") pytest.assume(2 == 2) print("加购操作") pytest.assume(3 == 2)
class TestCalc1(): def setup(self): self.cal = Calculator() @pytest.mark.add @pytest.mark.parametrize(('a', 'b', 'result'), yaml.safe_load(open(r'../datas/calc.yml'))['add']) def test_add(self, a, b, result, open1): print(f"计算数据: a={a}, b={b}, result={result}") assert self.cal.add(a, b) == result @pytest.mark.div @pytest.mark.parametrize(('a', 'b', 'result'), yaml.safe_load(open(r'../datas/calc.yml'))['div']) def test_div(self, a, b, result, open1): print(f"计算数据: a={a}, b={b}, result={result}") assert self.cal.div(a, b) == result @pytest.mark.mul @pytest.mark.parametrize(('a', 'b', 'result'), yaml.safe_load(open(r'../datas/calc.yml'))['mul']) def test_mul(self, a, b, result, open1): print(f"计算数据: a={a}, b={b}, result={result}") assert self.cal.mul(a, b) == result @pytest.mark.sub @pytest.mark.parametrize(('a', 'b', 'result'), yaml.safe_load(open(r'../datas/calc.yml'))['sub']) def test_sub(self, a, b, result, open1): print(f"计算数据: a={a}, b={b}, result={result}") assert self.cal.sub(a, b) == result
class TestCalc: def setup_class(self): self.cal = Calculator() @pytest.mark.parametrize( ['a', 'b', 'result'], yaml.safe_load(open("./data.yaml"))['add'], ids=['int', 'big num', 'xiaoshu', 'fushu', 'xiaoshu1']) def test_add(self, a, b, result): assert Decimal(result) == self.cal.add(a, b) @pytest.mark.parametrize(['a', 'b', 'result'], yaml.safe_load(open('./data.yaml'))['sub'], ids=['int', 'big num', 'xiaoshu', 'fushu']) def test_sub(self, a, b, result): assert Decimal(result) == self.cal.sub(a, b) @pytest.mark.parametrize(['a', 'b', 'result'], yaml.safe_load(open('./data.yaml'))['mul'], ids=['int', 'big num', 'xiaoshu', 'fushu']) def test_mul(self, a, b, result): assert Decimal(result) == self.cal.mul(a, b) @pytest.mark.parametrize(['a', 'b', 'result'], yaml.safe_load(open('./data.yaml'))['div'], ids=['int', 'big num', 'xiaoshu', 'fushu']) def test_div(self, a, b, result): assert Decimal(result) == self.cal.div(a, b)
class TestCalc: # 类级别 setup和teardown def setup_class(self): self.cal = Calculator() print("类级别 setup") def teardown_class(self): print("类级别 teardown") # 类里面的setup和teardown,运行在方法调用的前和后 # 每条类里面的测试用例前后分别执行 setup和teardown def setup(self): print("setup") def teardown(self): print("teardown") @pytest.mark.add @pytest.mark.parametrize('a,b,result', [(1, 1, 2), (2, 2, 4), (100, 100, 200), (0.1, 0.1, 0.2), (-1, -1, -2)], ids=["int", "int1", "bignun", "float", "fushu"]) def test_add(self, a, b, result): # cal = Calculator() assert result == self.cal.add(a, b) @pytest.mark.add def test_add1(self): # cal = Calculator() assert 3 == self.cal.add(1, 2) @pytest.mark.div def test_div(self): # cal = Calculator() assert 1 == self.cal.div(1, 1)
class CheckCalc(): def setup(self): self.cal = Calculator() # 加法 @pytest.mark.first @pytest.mark.dependency() @pytest.mark.parametrize("a,b,result", adddatas, ids=addids) def check_add(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.add(a, b) == result #除法 @pytest.mark.last @pytest.mark.dependency(depends=["test_mul"]) @pytest.mark.parametrize("a,b,result", divdatas, ids=divids) def check_div(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.div(a, b) == result # 减法 @pytest.mark.second @pytest.mark.dependency(depends=["test_add"]) @pytest.mark.parametrize("a,b,result", subdatas, ids=subids) def check_sub(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.sub(a, b) == result #乘法 @pytest.mark.third @pytest.mark.dependency() @pytest.mark.parametrize("a,b,result", muldatas, ids=mulids) def check_mul(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.mul(a, b) == result
class TestCalc: def setup_class(self): self.cal = Calculator() @pytest.mark.run(order=2) @pytest.mark.dependency(name="add") # @pytest.mark.xfail(reason="deliberate fail") @pytest.mark.parametrize('read', c, ids=ids_add, indirect=True) def check_add(self, read): print(f"计算:{read[0]} + {read[1]},预期结果为:{read[2]}") try: temp_result = self.cal.add(read[0], read[1]) assert temp_result == read[2] except Exception as e: print(e) assert False @pytest.mark.run(order=3) @pytest.mark.dependency(depends=["add"]) @pytest.mark.parametrize('read', c, ids=ids_sub, indirect=True) def check_sub(self, read): print(f"计算:{read[0]} - {read[1]},预期结果为:{read[3]}") try: temp_result = self.cal.sub(read[0], read[1]) assert temp_result == read[3] except Exception as e: print(e) assert False @pytest.mark.run(order=4) @pytest.mark.dependency(name="mul") @pytest.mark.parametrize('read', c, ids=ids_mul, indirect=True) def check_mul(self, read): print(f"计算:{read[0]} * {read[1]},预期结果为:{read[4]}") try: temp_result = self.cal.mul(read[0], read[1]) assert temp_result == read[4] except Exception as e: print(e) assert False @pytest.mark.run(order=5) @pytest.mark.dependency(depends=["mul"]) @pytest.mark.parametrize('read', c, ids=ids_div, indirect=True) def check_div(self, read): print(f"计算:{read[0]} ÷ {read[1]},预期结果为:{read[5]}") try: temp_result = self.cal.div(read[0], read[1]) assert temp_result == read[5] except Exception as e: print(e) assert False
class TestCalc: # 定义setup_class()方法 def setup_class(self): print("开始计算") # 实例化计算器 self.calc = Calculator() # 定义teardown_class()方法 def teardowm_class(self): print("结束计算") # 定义setup()方法 def setup(self): print("要灵活使用") # 定义teardown()方法 def teardowm(self): print("灵活使用结束") # 装饰器参数化 加法标记 @pytest.mark.add @pytest.mark.parametrize('a,b,expect',[ (1,1,2), (100,100,200), (0.1,0.1,0.2), (-1,-1,-2), ],ids=['int','bigint','float','minus']) # 定义一个test_add1()方法 def test_add(self,a,b,expect): # 调用它的相加add()方法 result=self.calc.add(a,b) # 判断result为小数的时候使用round取小数点后两位 if isinstance(result,float): result=round(result,2) # 断言 assert expect == result # 装饰器 yaml文件传入参数化 @pytest.mark.div @pytest.mark.parametrize('a ,b,hope',mydatas,ids=myid) # 定义除法测试方法,传入参数和希望值 def test_div(self,a,b,hope): # 除法结果 result=self.calc.div(a,b) # 断言希望值是否等于除法结果 assert hope == result
class Testcalc: def setup_class(self): self.calc = Calculator() print("开始计算") def teardown_class(self): print("计算结束") @pytest.mark.parametrize('a,b,expect', adddatas, ids=myid) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert expect == result @pytest.mark.parametrize('a,b,expect', divdatas, ids=myid1) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert expect == result
class TestCalc: # setup_class,teardown_class 类级别每个类里面执行前后分别执行 def setup_class(self): self.cal = Calculator() print('类级别setup') def teardown_class(self): print('类级别teardown') # 方法级别,每个方法里面的测试用例前后分别执行setup、teardown def setup(self): # self.cal = Calculator() print('setup') def teardown(self): print('teardown') # 方法级别,每个方法里面的测试用例前后分别执行setup、teardown def setup_method(self): # self.cal = Calculator() print('方法级别setup') def teardown_method(self): print('方法级别teardown') @pytest.mark.add @pytest.mark.parametrize('a,b,result', [(1, 1, 3), (2, 2, 4), (100, 100, 200), (0.1, 0.1, 0.2), (-1, -1, -2)], ids=['int', 'int', 'bignum', 'float', '负数']) # 参数化 def test_add(self, a, b, result): # cal = Calculator() assert result == self.cal.add(a, b) # @pytest.mark.add # @pytest.mark.parametrize('a,b,result', mydatas, ids=myids) # def test_add1(self,a,b,result): # # cal = Calculator() # assert result == self.cal.add(a, b) @pytest.mark.div def test_div(self): # cal = Calculator() assert 1 == self.cal.div(1, 1)
class TestCalc(): def setup_class(self): self.cal = Calculator() def teardown_class(self): pass # @pytest.mark.parametrize("a, b, result",[ # (1,1,2), # (-1,-1,-2), # (-1,1,0) # ],) @pytest.mark.run(order=1) @pytest.mark.parametrize(('a,b,result'), add_datas, ids=adds) @pytest.mark.add def test_add(self, a, b, result): assert result == self.cal.add(a, b) # 插件pytest-dependency 控制用例依赖关系 ''' 如果test_add用例成功,test_cut会被执行 如果test_add用例失败,test_cut会被跳过不被执行 depends = [] 列表中加入依赖得到用例名称 ''' # 插件pytest-ordering控制用例执行顺序 @pytest.mark.run(order=2) @pytest.mark.dependency(depends=['test_add'], name='test_cut') @pytest.mark.parametrize('a,b,result', cut_datas, ids=cuts) @pytest.mark.cut def test_cut(self, a, b, result): assert result == self.cal.cut(a, b) @pytest.mark.run(order=3) @pytest.mark.parametrize('a,b,result', mult_datas, ids=mults) @pytest.mark.mult def test_mult(self, a, b, result): assert result == self.cal.mult(a, b) @pytest.mark.run(order=4) @pytest.mark.dependency(depends=['test_mult'], name='test_div') @pytest.mark.parametrize('a,b,result', div_datas, ids=divs) @pytest.mark.div def test_div(self, a, b, result): assert result == self.cal.div(a, b)
def test_div(self,a,b,c): """ 除法测试用例 :param a: :param b: :param c: :return: """ with allure.step("实例化"): cal = Calculator() with allure.step("判断除数是否为0"): if b != 0: print(f'\na,b,c的值:{a} / {b} = {c}') assert cal.div(a,b) == c else: allure.attach("<body>这是一段html body块</body>", "html测试块", attachment_type=allure.attachment_type.HTML) allure.attach.file("../report/photo/a.jpg", name="这是图片", attachment_type=allure.attachment_type.JPG) allure.attach("除数不能为0!", attachment_type=allure.attachment_type.TEXT) print("除数不能为0!")
class TestCalc: def setup_method(self): self.cal = Calculator() print("setup_method") @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5)]) @pytest.mark.add def test_add(self, a, b, result, open): #cal = Calculator() assert self.cal.add(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.div def test_div(self, a, b, result, open): cal = Calculator() assert self.cal.div(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.subtraction def test_subtraction(self, a, b, result, open): #cal = Calculator() assert self.cal.subtraction(a, b) == result @pytest.mark.parametrize(("a", "b", "result"), [(1, 2, 3), (-1, 2, 1), (-1, -2, 3), (0, 0, 0), (0.5, 3, 3.5), (1, 0, 0), (0, 1, 0)]) @pytest.mark.multiplication def test_multiplication(self, a, b, result, open): assert self.cal.multiplication(a, b) == result
class CheckCalc(): def setup(self): self.cal = Calculator() @pytest.mark.parametrize("a,b,result", adddatas, ids=addids) def check_add(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.add(a, b) == result @pytest.mark.parametrize("a,b,result", divdatas, ids=divids) def check_div(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.div(a, b) == result @pytest.mark.parametrize("a,b,result", subdatas, ids=subids) def check_sub(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.sub(a, b) == result @pytest.mark.parametrize("a,b,result", muldatas, ids=mulids) def check_mul(self, a, b, result): print(f"\n计算数据:a={a},b={b},result={result}") assert self.cal.mul(a, b) == result
class TestCalc: # 类里面执行 def setup_class(self): print("类级别setup") self.calc = Calculator() def teardown_class(self): print("类级别teardown") @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['add']) def test_add(self, list1, calc): add_result = self.calc.add(list1[0], list1[1]) print(f'实际结果为:{add_result}, 类型为{type(add_result)}') assert Decimal(list1[2]).quantize(Decimal('0.00')) == add_result @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['div']) def test_div(self, list1, calc): assert Decimal(list1[2]).quantize(Decimal('0.00')) == self.calc.div( list1[0], list1[1]) @pytest.mark.parametrize('list1', yaml.safe_load(open('./../calc.yml'))['decrease']) def test_decrease(self, list1, calc): assert Decimal(list1[2]).quantize( Decimal('0.00')) == self.calc.decrease(list1[0], list1[1]) @pytest.mark.parametrize('list1', yaml.safe_load( open('./../calc.yml'))['multiplication']) def test_multiplication(self, list1, calc): print(list1) assert Decimal(list1[2]).quantize( Decimal('0.00')) == self.calc.multiplication(list1[0], list1[1])
class TestCal: def setup(self): self.cal = Calculator() print("初始化一个计算机对象") def teardown(self): print("结束") @pytest.mark.add @pytest.mark.parametrize('a,b,result', [(1, 1, 2), (0.2, 0.2, 0.4)], ids=['int', 'float']) #给用例命名 def test_add01(self, a, b, result): # cal = Calculator() assert result == self.cal.add(a, b) @pytest.mark.add def test_add02(self): # cal = Calculator() assert 4 == self.cal.add(2, 2) @pytest.mark.div def test_div(self): # cal = Calculator() assert 1 == self.cal.div(2, 2)
class TestCal: def setup(self): self.calc = Calculator() def test_add(self): # 加法 - 正整数与正整数 assert 3 == self.calc.add(1, 2) def test_add1(self): # 加法 - 正整数与负整数 assert 20 == self.calc.add(30, -10) def test_add2(self): # 加法 - 正整数与0 assert 30 == self.calc.add(30, 0) def test_add3(self): # 加法 - 正整数与浮点数 assert 31.5 == self.calc.add(30, 1.5) def test_add4(self): # 加法 - 正整数与复数 assert 31 + 5j == self.calc.add(30, 1 + 5j) def test_add5(self): # 加法 - 负整数与0 assert -29 + 5j == self.calc.add(-30, 1 + 5j) def test_add6(self): # 加法 - 浮点数与浮点数 assert 3 == self.calc.add(1.5, 1.5) def test_add7(self): # 加法 - 浮点数与0 assert 1.5 == self.calc.add(1.5, 0) def test_add8(self): # 加法 - 复数与复数 assert -29 + 5j == self.calc.add(-30 + 2j, 1 + 3j) def test_add9(self): # 加法 - 复数与浮点数 assert -29.5 + 5j == self.calc.add(-30.5 + 2j, 1 + 3j) def test_add10(self): # 加法 - 结果错误 assert -29.5 + 5j != self.calc.add(-40.5 + 2j, 1 + 3j) def test_div(self): # 除法-整数与整数 assert 3 == self.calc.div(6, 2) @pytest.mark.xfail def test_div1(self): # 除法-整数与0 assert 1 != self.calc.div(6, 0) def test_div2(self): # 除法-整数与浮点数 assert 4 == self.calc.div(6, 1.5) def test_div3(self): # 除法-正数与负数 assert -3 == self.calc.div(6, -2) def test_div4(self): # 除法-整数与复数 assert 4 != self.calc.div(6, 1.5 + 5j)
def test_div(self): cal = Calculator() assert cal.div(4, 2) == 2
def test_div(self, a, b, forward): cal = Calculator() assert a / b == cal.div(a, b)
def test_div(): cal = Calculator() assert 1 == cal.div(2,2)
def test_div2(): cal = Calculator() assert cal.div(2, 1) == 2