class TestCalculator: def setup_class(self): print("开始计算") self.calc = Calculator() def teardown_class(self): print("计算结束") self.calc = Calculator() @pytest.mark.parametrize('a,b,expect', get_adddatas()[0], ids=get_adddatas()[1]) def test_add_case(self, a, b, expect): print("这是加法的测试用例") result = self.calc.add(a, b) assert expect == result print("运算成功") @pytest.mark.parametrize('a,b,expect', get_divdatas()[0], ids=get_divdatas()[1]) def test_div_case(self, a, b, expect): print("这是除法的测试用例") result = self.calc.div(a, b) assert expect == result print("运算成功")
class TestCalc: def setup_class(self): self.cal = Calculator() print("开始计算") def teardown_class(self): print("计算结束") @pytest.mark.parametrize("a,b,expect", [(3, 5, 8), (-1, -2, -3), (100, 200, 300)], ids=["int", "minus", "bigint"]) def test_add(self, a, b, expect): assert expect == self.cal.add(a, b) @pytest.mark.parametrize("a,b,expect", [(4, 8, -4), (-6, -5, -1), (99, 108, -9)], ids=["int", "minus", "bigint"]) def test_sub(self, a, b, expect): assert expect == self.cal.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(4, 8, 32), (-6, -5, 30), (99, 108, 10692)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.cal.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(60, 5, 12), (-6, -5, 1.2), (500, 100, 5)], ids=["int", "minus", "bigint"]) def test_div(self, a, b, expect): assert expect == self.cal.div(a, b)
class TestCase(): def setup_class(self): self.calc = Calculator() def teardown_class(self): pass @pytest.mark.run(order=1) @pytest.mark.parametrize("a,b,expect",*read_yaml("add")) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect @pytest.mark.run(order=2) @pytest.mark.parametrize("a,b,expect",*read_yaml("sub")) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect @pytest.mark.run(order=3) @pytest.mark.parametrize("a,b,expect",*read_yaml("mul")) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect @pytest.mark.run(order=4) @pytest.mark.parametrize("a,b,expect",*read_yaml("div")) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect
class TestCalc: def setup_class(self): self.calc = Calculator() print('Start calculation...') def teardown_class(self): print('End calculation') @pytest.mark.parametrize('a,b,expect', [(3, 5, 8), (-1, -2, -3), (100, 300, 500)], ids=['int', 'minus', 'bigint']) def test_add(self, a, b, expect): assert expect == self.calc.add(a, b) @pytest.mark.parametrize('a,b,expect', [(3, 5, -2), (-1, -2, 1), (300, 300, 10)], ids=['int', 'minus', 'bigint']) def test_minus(self, a, b, expect): assert expect == self.calc.minus(a, b) @pytest.mark.parametrize('a,b,expect', [(3, 5, 12), (-1, -2, 2), (300, 4, 1200)], ids=['int', 'minus', 'bigint']) def test_mul(self, a, b, expect): assert expect == self.calc.mul(a, b) @pytest.mark.parametrize('a,b,expect', [(3, 5, 0.6), (-4, -2, 2), (400, 100, 3)], ids=['int', 'minus', 'bigint']) def test_div(self, a, b, expect): assert expect == self.calc.div(a, b)
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, 101], [0.1, 0.1, 0.2], [-1, -1, -2], [1, 0, 1], [0, 1, 1], [-1, 1, 0], [-100, -400, -500], [-500, 1000, 500]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case', "zero_int_case", "minus_int_case", "minus_bignum_case", "minus_bignum_int_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', [[1, 1, 1], [100, 2, 50], [0.8, 2, 0.4], [1.2, 0.1, 12], [-100, -2, 50], [10000, 100, 100], [15, 2, 7.5], [0, 10, 0]], ids=[ 'int_case', 'int_bignum_case', 'float_case', 'float_float_case', 'minus_case', 'bignum_case', 'int_folat_case', 'zero_case' ]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect
class TestCalc(): def setup_method(self): print("开始计算method") @classmethod def setup_class(self): self.calc = Calculator() print("开始计算") @classmethod def teardown_class(self): print("\n结束计算") def teardown_method(self): print("结束计算method") @pytest.mark.parametrize("a,b,expect",[(3,5,8),(-1,-2,-3),(100,200,300)],ids=["int","minus","bigint"]) def test_add(self,a,b,expect): assert expect == self.calc.add(a,b) @pytest.mark.parametrize("a,b,expect", [(5, 3, 2), (-5, -2, -3), (300, 200, 100)], ids=["int", "minus", "bigint"]) def test_sub(self,a,b,expect): assert expect == self.calc.sub(a,b) @pytest.mark.parametrize("a,b,expect",[(5,2,10),(5.5,10,55),(100,20,2000)],ids=['int','float','bigint']) def test_mul(self,a,b,expect): assert expect == self.calc.mul(a,b) @pytest.mark.parametrize("a,b,expect",[(10,2,5),(10.2,2,5.1),(2000,20,100)],ids=['int','float','bigint']) def test_div(self,a,b,expect): assert expect == self.calc.div(a,b)
class TestCalc: def setup_class(self): self.calc = Calculator() print('【开始计算】') def teardown_class(self): print('【计算结束】') @pytest.mark.parametrize('a,b,expect', [(3, 5, 8), (-1, -2, -3), (100, 300, 400)], ids=['int', 'minus', 'bigint']) def test_add(self, a, b, expect): # cal=Calculator() assert expect == self.calc.add(a, b) @pytest.mark.parametrize('a,b,expect', [(5, 4, 1), (200, 100, 100), (10, 6, 4)]) def test_sub(self, a, b, expect): # cal=Calculator() #与上方add都用了调用计算函数,抽取出来放至setup assert expect == self.calc.sub(a, b) @pytest.mark.parametrize('a,b,expect', [(5, 4, 20), (20, 10, 200), (10, 6, 60)]) def test_mul(self, a, b, expect): assert expect == self.calc.mul(a, b) @pytest.mark.parametrize('a,b,expect', [(8, 4, 2), (200, 100, 2), (10, 2, 5)]) def test_div(self, a, b, expect): assert expect == self.calc.div(a, b)
class TestCal(Calculator): def setup(self): self.calc = Calculator() @pytest.mark.parametrize("a, b, expect", get_datas()[0], ids=get_datas()[1]) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect @pytest.mark.flaky(reruns=3, reruns_dalay=5) def test_sub(self): result = self.calc.sub(5, 3) assert result == 5 def test_mul(self): result = self.calc.mul(3, 7) assert result == 21 def test_div(self): result = self.calc.div(6, 2) assert result == 3 def test_add_steps(self): a = 1 b = 2 expect = 3 # assert 2 == self.calc.add(1, 1) # assert 3 == self.calc.add1(1, 2) # assert 0 == self.calc.add(-1, 1) steps(self.calc, a, b, expect)
class TestCalc: def setup_class(self): self.calc = Calculator() @pytest.mark.parametrize("a,b,expect", get_datas()[0]) def test_add(self, a, b, expect): # 调用add函数,返回的结果保存在result里面 result = self.calc.add(a, b) # 判断result结果是否等于期望的值 assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()[1]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()[2]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()[3]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect
class TestCalculator: def setup_class(self): print("start") self.cal = Calculator() @pytest.mark.parametrize("a, b, excepted", [(1, 2, 3), (-1, 1, 0), (1.03, 1.23, 2.26)]) def test_add(self, a, b, excepted): print("add") assert self.cal.add(a, b) == excepted @pytest.mark.parametrize("a, b, excepted", yaml.safe_load(open("./data.yml"))["data"]) def test_sub(self, a, b, excepted): print("sub") print(yaml.safe_load(open("./data.yml"))["data"]) res = self.cal.sub(a, b) if isinstance(res, float): res = round(res, 2) assert res == excepted @pytest.mark.parametrize("a, b, excepted", yaml.safe_load(open("./data.yml"))["mul"]) def test_mul(self, a, b, excepted): print("mul") print(yaml.safe_load(open("./data.yml"))["mul"]) assert self.cal.mul(a, b) == excepted @pytest.mark.parametrize("a, b, excepted", yaml.safe_load(open("./data.yml"))["div"], ids=yaml.safe_load(open("./data.yml"))["divids"]) def test_dev(self, a, b, excepted): print("div") print(yaml.safe_load(open("./data.yml"))["div"]) assert self.cal.div(a, b) == excepted
class TestDiv: def setup_class(self): self.cal = Calculator() @pytest.mark.run(order=3) @pytest.mark.parametrize("a,b,expect", get_data()["div_data"]["formal"]) def test_div_formal(self, a, b, expect): result = self.cal.div(a, b) assert (result) == expect @pytest.mark.run(order=4) @pytest.mark.parametrize("a,b", get_data()["div_data"]["zero"]) def test_div_zero(self, a, b): try: result = self.cal.div(a, b) except ZeroDivisionError: print("除数为0")
class TestCalc(): # calc = Calculator() def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize( 'a,b,c', [[1, 1, 2], [100, 100, 200], [1 / 3, 4 / 7, 19 / 21], [-1, -2, -3], [1, 0, 1]], ids=[ 'int_case', 'bigNum_case', 'float_case', 'minus_case', 'zero_case' ]) def test_add(self, a, b, c): result = self.calc.add(a, b) assert result == round(c, 2) def test_add1(self): test_data = [[1, 2, 3], [100, 1000, 1100], [0.56, 0.67, 1.23], [-3, -4.5, -7.5], [0, 3, 3]] # print (test_data[1]) for i in range(0, len(test_data)): result = self.calc.add(test_data[i][0], test_data[i][1]) assert result == test_data[i][2] def test_sub(self): result = self.calc.sub(3, 1) assert result == 2 def test_mul(self): result = self.calc.mul(3, 3) assert result == 9 ##方法round @pytest.mark.parametrize( 'e,d,f', [[4, 2, 2], [0.1, 0.1, 1], [0, 5, 0], [1, 0, 3], [1, 3, 0.33], [2, 3, 0.67]], ids=[u'整数相处', u'小数相除', u'分子为零', u'分母为零', u'不四舍五入', u'四舍五入']) def test_div(self, e, d, f): result = self.calc.div(e, d) assert result == f ##Decimal @pytest.mark.parametrize( 'e,d,f', [[4, 2, 2], [0.1, 0.1, 1], [0, 5, 0], [1, 0, 3], [1, 3, 0.33], [2, 3, 0.67]], ids=[u"整数相处", u"小数相除", u'分子为零', u'分母为零', u'不四舍五入', u'四舍五入']) def test_div(self, e, d, f): result = self.calc.div1(e, d) assert result == Decimal(f).quantize(Decimal('0.00')) def a(self): b = get_datas() print(b)
class TestCal(): def setup_class(self): self.cal = Calculator() print("测试开始") def teardown_class(self): print("测试结束") @pytest.mark.add def test_add(self): # cal = Calculator() assert 3 == self.cal.add(1, 2) # 加上ids参数,表示给测试用例起别名 # 以下参数使用yaml进行了参数化,已经移到datas/calc.yml中,然后使用参数mydatas和myids进行的替换 @pytest.mark.parametrize( 'a, b, result', adddatas # [ # (1,2,3), # (100,50,150), # (0.1, 0.1, 0.2), # (-1,-1,-2) # ] # ,ids=['int','bignum','float','fushu'] , ids=addids) @pytest.mark.add def test_add1(self, 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) steps(a, b, result) @pytest.mark.parametrize('a, b, result', subdatas, ids=subids) def test_sub(self, result, a, b): assert result == self.cal.sub(a, b) # 加上@pytest.mark.flaky()装饰器,表示失败重试, # reruns=3表示失败重试次数,reruns_delay=2表示两次重试之间的延迟时间 @pytest.mark.flaky(reruns=3, reruns_delay=2) @pytest.mark.div def test_div1(self): # cal = Calculator() assert 2 == self.cal.div(4, 2) # assume断言,前面的失败后,后面的断言还会继续执行 # assert 断言,第一条失败后,后面将停止运行 def test_assume(self): print("登录操作") pytest.assume(1 == 2) print("搜索操作") pytest.assume(2 == 2) print("加购操作") pytest.assume(3 == 3)
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, 100, 200], [0.1, 0.1, 0.2], [-1, -1, -2], [1, 0, 1], [-1, 1, 0], [0.9999999999, 0, 0.9999999999], [-1.1, 1, -0.1] ] #, ids=['int_case','bignum_case','flast_case','minus_case','zero_case'] ) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect @pytest.mark.parametrize('a,b,expect', [ [100, 100, 1], [100, -1, -100], [1, 0, ''], [0, 100, 0], [0.1, 0.2, 0.5], [0.1, 0.02, 5], [-1, -1, 1], [1, 3, 0.3], [1, '', 0.3] ] # , ids=['整数相除','整数除以负数','除数为0','被除数为0','小数相除','小数相除为整数','负负得正','除不尽'] ) def test_dev(self,a,b,expect): result=self.calc.div(a,b) assert result == expect # def test_add1(self): # #calc=Calculator() # result = self.calc.add(100,100) # assert result == 200 # # def test_add2(self): # #calc=Calculator() # result = self.calc.add(0.1,0.1) # assert result == 0.2
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, 100, 200], [0.1, 0.1, 0.2], [-1, -1, -2], [1, 0, 1]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_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', [[1, 1, 0], [200, 100, 100], [0.2, 0.1, 0.1], [-1, -1, 0], [0, 0, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'Minus_case', 'zero_case' ]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect # 乘法 @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 1], [200, 100, 20000], [0.2, 1, 0.2], [-1, -1, 1], [0, 0, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'Minus_case', 'zero_case' ]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect # 除法 @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 1], [200, 100, 2], [0.2, 1, 0.2], [-1, -1, 1], [5, 0, '除数不能为0']], ids=['整数测试用例', 'bignum_case', 'float_case', 'Minus_case', 'zero_case']) def test_div(self, a, b, expect): if b == 0: print("除数不能为0!") else: result = self.calc.div(a, b) 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], [1000, 10000, 11000], [0.1, 0.3, 0.4], [-1, -1, -2], [0, 0, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case' ]) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect #数字+字符串 def test_add1(self): try: result = self.calc.add(1, '1') except TypeError as e: print("type error", e) #除法测试 @pytest.mark.parametrize( 'a,b,expect', [[-1 - 1j, 2, -0.5 - 0.5j], [1 + 1j, 2, 0.5 + 0.5j], [-1, 2, -0.5], [0, 2, 0], [1, 2, 0.5], [2, 2, 1], [10, 3, 10 / 3], [10, 2, 5], [100000, 2, 50000]]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect #除数为0 def test_div1(self): try: result = self.calc.div(1, 0) except ZeroDivisionError as e: print("ZeroDivisionError", e)
class TestCalc1: def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 1], [0, 1, 0], [10, 1, 10], [-10, 1, -10], [10, -1, -10], [-10, -1, 10], [1, 2, 0.5], [1, 3, 0.3333333333333333], [1, 100, 0.01], [0.1, 0.1, 1], [0.1, 1, 0.1], [0.1, 0.001, 100]], ids=[ 'float1_case', 'float2_case', 'float3_case', 'float4_case', 'float5_case', 'float6_case', 'float7_case' ]) def test_div(self, a, b, expect): # calc = Calculator() result = self.calc.div(a, b) assert result == expect @pytest.mark.parametrize('a,b', [[0.1, 0], [10, 0]]) def test_div(self, a, b): with pytest.raises(ZeroDivisionError): # 补货异常场景 self.calc.div(a, b) # def test_div(self): # with pytest.raises(ZeroDivisionError) : #补货异常场景 # result = self.calc.div(1,0) # try: # result = self.calc.div(1,0) # except ZeroDivisionError : # print("错误") def test_add_setup(self): a = 1 b = 1 expect = 2 setup("./setup/add_setup.yml", self.calc, a, b, expect)
class TestCalc: def setup_class(self): # 实例化类,生成类的对象 self.calc = Calculator() def setup_method(self): log.logger.debug("\nsetup_module:【开始计算】") def teardown_method(self): log.logger.debug("\nsetup_module:【结束计算】") # 使用参数化 @pytest.mark.parametrize("a,b,expect", get_datas()[0][0], ids=get_datas()[1][0]) # 测试add函数 def test_add(self, a, b, expect): # 调用add函数,返回的结果保存在result里面 result = self.calc.add(a, b) # 判断result结果是否等于期望的值 assert result == expect log.logger.debug({f"预期结果:{result}"}) # 测试sub函数 @pytest.mark.parametrize("a,b,expect", get_datas()[0][1], ids=get_datas()[1][1]) def test_sub(self, a, b, expect): # 调用sub函数,返回的结果保存在result里面 result = self.calc.sub(a, b) assert result == expect log.logger.debug({f"预期结果:{result}"}) # 测试mul函数 @pytest.mark.parametrize("a,b,expect", get_datas()[0][2], ids=get_datas()[1][2]) def test_mul(self, a, b, expect): # 调用mul函数,返回的结果保存在result里面 result = self.calc.mul(a, b) assert result == expect log.logger.debug({f"预期结果:{result}"}) # 测试div函数 @pytest.mark.parametrize("a,b,expect", get_datas()[0][3], ids=get_datas()[1][3]) def test_div(self, a, b, expect): # 调用div函数,返回的结果保存在result里面 result = self.calc.div(a, b) assert result == expect log.logger.debug({f"预期结果:{result}"})
class TestCalc(): def setup_class(self): self.calc = Calculator() print("测试开始") 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.5, 1.5]], ids=[ 'int_case_pass', 'int_case_fail', 'float_case_pass', 'minus_case_pass', 'zero_case_pass' ]) def test_add(self, a, b, expect): result = self.calc.add(a, b) print(123123121) assert result == expect @pytest.mark.parametrize( "a,b,expect", [[1, 2, 3], [100, 200, 300], [0.1, 0.1, 0.2], [-1, -2, -3], [1, 0, 1]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case' ]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", [[1, 2, 2], [300, 400, 120000], [0.1, 0.9, 0.09], [-1, -3, 3], [4, 0, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case' ]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", [[4, 2, 2], [1000, 200, 5], [0.9, 0.3, 3], [-9, -3, 3], [9, 0, '除数不能为0'], [0, 9, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_1_case', 'zero_1_case' ]) def test_div(self, a, b, expect): result = self.calc.div(a, b) 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], [100, 100, 200], [-1, -1, -2], [1, 0, 1]], ids=['int_case', 'bignum_case', 'float_case', 'zero_case']) def test_add(self, a, b, expect): 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', [[0.1, 0], [10, 0]]) def test_dev_zero(self, a, b): with pytest.raises(ZeroDivisionError): self.calc.div(1, 0)
class TestCalc(): def setup_class(self): self.calc = Calculator() print("开始计算") def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,expect", [(3, 5, 8), (-1, -2, -3), (100, 300, 400)], ids=["中文int", "minus", "bigint"]) def test_add(self, a, b, expect): assert expect == self.calc.add(a, b) def test_div(self): assert 1 == self.calc.div(1, 1)
class Testcalcu(): def setup_class(self): self.cal=Calculator() def teardown_class(self): print('计算器类测试结束') @pytest.mark.parametrize('a,b,expect',[(1,1,2),(-1,-2,-3),(0.5,0.1,0.6)],ids=['+','-','.']) def test_add(self,a,b,expect): assert self.cal.add(a,b)==expect @pytest.mark.parametrize('a,b,expect', [(1, 1, 0), (-1, -2, 1), (0.5, 0.1, 0.4)], ids=['+', '-', '.']) def test_sub(self,a,b,expect): assert self.cal.sub(a,b)==expect @pytest.mark.parametrize('a,b,expect', [(1, 1, 1), (-1, -2, 2), (0.5, 0.1, 0.05)], ids=['+', '-', '.']) def test_mul(self,a,b,expect): assert self.cal.mul(a,b)==expect @pytest.mark.parametrize('a,b,expect', [(1, 1, 1), (-1, -2, 0.5), (0.5, 0.1, 5)], ids=['+', '-', '.']) def test_div(self,a,b,expect): assert self.cal.div(a,b)==expect
class TestCalc: def setup_method(self): print("\n开始计算") def teardown_method(self): print("\n结束计算") def setup_class(self): # 实例化类,生成类的对象 self.calc = Calculator() # 使用参数化 @pytest.mark.parametrize("a,b,expect", get_datax()[0], ids=get_datax()[1]) # 测试add函数 def test_add(self, a, b, expect): # 调用add函数,返回的结果保存在result里面 result = self.calc.add(a, b) # 判断result结果是否等于期望的值 assert result == expect # 使用参数化 @pytest.mark.parametrize("a,b,expect", get_datax()[2], ids=get_datax()[3]) # 测试sub函数 def test_sub(self, a, b, expect): # 调用sub函数,返回的结果保存在result里面 result = self.calc.sub(a, b) # 判断result结果是否等于期望的值 assert result == expect @pytest.mark.parametrize("a,b,expect", get_datax()[4], ids=get_datax()[5]) # 测试sub函数 def test_mul(self, a, b, expect): # 调用mul函数,返回的结果保存在result里面 result = self.calc.mul(a, b) # 判断result结果是否等于期望的值 assert result == expect @pytest.mark.parametrize("a,b,expect", get_datax()[6], ids=get_datax()[7]) # 测试sub函数 def test_div(self, a, b, expect): # 调用div函数,返回的结果保存在result里面 result = self.calc.div(a, b) # 判断result结果是否等于期望的值 assert result == expect
class TestCal: def setup_class(self): self.calc = Calculator() print("开始测试:加、减、乘、除") def teardown_class(self): print("加、减、乘、除 的测试结束") def setup_method(self): print("开始计算") def teardown_method(self): print("计算结束\n") @pytest.mark.parametrize("a,b,expect", [(0, 1, 1), (3, 5, 8), (-1, -1, -2), (1000, 1000, 2000)], ids=["zero", "int", "mius", "bigint"]) def test_add(self, a, b, expect): print(f"加运算:{a} + {b} = {expect}") assert expect == self.calc.add(a, b) @pytest.mark.parametrize("a,b,expect", [(5, 0, 5), (2, 1, 1), (-5, -1, -4), (20000, 9999, 10001)], ids=["zero", "int", "mius", "bigint"]) def test_sub(self, a, b, expect): print(f"减运算:{a} - {b} = {expect}") assert expect == self.calc.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(1, 0, 0), (1, 1, 1), (-1, -1, 1), (2021, 2000, 4042000)], ids=["zero", "int", "mius", "bigint"]) def test_mul(self, a, b, expect): print(f"乘运算:{a} * {b} = {expect}") assert expect == self.calc.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(1, 0, 0), (0, 9, 0), (1, 1, 1), (-100, -2, 50), (10000, 100, 100)], ids=["zero", "zero1", "int", "mius", "bigint"]) def test_div(self, a, b, expect): print(f"除运算:{a} / {b} = {expect}") if b != 0: assert expect == self.calc.div(a, b) else: print("除数不能为0")
class TestCal: def setup_class(self): self.calc = Calculator() print("开始测试:加、减、乘、除") def teardown_class(self): print("加、减、乘、除 的测试结束") def setup_method(self): print("开始计算") def teardown_method(self): print("计算结束\n") @pytest.mark.parametrize("a,b,expect", get_datas()["add_datas"], ids=get_datas()["my_addid"]) def test_add(self, a, b, expect): print(f"加运算:{a} + {b} = {expect}") assert expect == self.calc.add(a, b) @pytest.mark.parametrize("a,b,expect", get_datas()["sub_datas"], ids=get_datas()["my_subid"]) def test_sub(self, a, b, expect): print(f"减运算:{a} - {b} = {expect}") assert expect == self.calc.sub(a, b) @pytest.mark.parametrize("a,b,expect", get_datas()["mul_datas"], ids=get_datas()["my_mulid"]) def test_mul(self, a, b, expect): print(f"乘运算:{a} * {b} = {expect}") assert expect == self.calc.mul(a, b) @pytest.mark.parametrize("a,b,expect", get_datas()["div_datas"], ids=get_datas()["my_divid"]) def test_div(self, a, b, expect): print(f"除运算:{a} / {b} = {expect}") if b != 0: assert expect == self.calc.div(a, b) else: print("除数不能为0")
class TestCalc: def setup_class(self): # 实例化类对象 self.calc = Calculator() def setup_method(self): print("\n【开始计算】") def teardown_method(self): print("\n【计算结束】") @pytest.mark.parametrize("a, b, expect", get_data()["add_datas"], ids=get_data()["add_myids"]) def test_add(self, a, b, expect): res = self.calc.add(a, b) print("实际计算结果为:", res) assert res == expect @pytest.mark.parametrize("a, b, expect", get_data()["sub_datas"], ids=get_data()["sub_myids"]) def test_sub(self, a, b, expect): res = self.calc.sub(a, b) print("实际计算结果为:", res) assert res == expect @pytest.mark.parametrize("a, b, expect", get_data()["mul_datas"], ids=get_data()["mul_myids"]) def test_mul(self, a, b, expect): res = self.calc.mul(a, b) print("实际计算结果为:", res) assert res == expect @pytest.mark.parametrize("a, b, expect", get_data()["div_datas"], ids=get_data()["div_myids"]) def test_div(self, a, b, expect): res = self.calc.div(a, b) print("实际计算结果为:", res) assert res == expect
class TestCalc: def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") # def test_add(self): # #calc = Calculator() # result = self.calc.add(1,1) # assert result == 2 @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 2], [1000, 1000, 2000], [0.1, 0.2, 0.3], [-1, -1, -2]]) def test_add1(self, a, b, expect): #calc = Calculator() result = self.calc.add(a, b) assert round(result, 2) == expect @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 0], [1000, 100, 900], [0.3, 0.2, 0.3], [-1, -1, 0]]) def test_sub(self, a, b, expect): #calc = Calculator() result = self.calc.sub(a, b) assert result == expect @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 1], [1000, 100, 100000], [0.3, 0.1, 0.03], [-1, -1, 1]]) def test_mul(self, a, b, expect): # calc = Calculator() result = self.calc.mul(a, b) assert result == expect # 除数为0的情况下 @pytest.mark.parametrize('a,b', [[1, 0], [1000, 0], [0.3, 0], [-1, 0]]) def test_div1(self, a, b): with pytest.raises(ZeroDivisionError): result = self.calc.div(a, b)
class TestCalc: def setup_class(self): # 实例化类,生成类的对象 self.calc = Calculator() print("\nsetup_class:所有计算开始") def setup_method(self): print("\nsetup_method:计算开始") def teardown_method(self): print("\nteardown_method:计算结束") def teardown_class(self): print("\nteardown_class:所有计算结束") # 使用参数化 @pytest.mark.parametrize("a,b,expect", yaml.safe_load(open("./data.yml"))["datas"]) # 测试add函数 def test_add(self, a, b, expect): # 调用add函数,返回的结果保存在result里面 result = self.calc.add(a, b) # 判断result结果是否等于期望的值 assert result == expect @pytest.mark.parametrize("a,b,expect", yaml.safe_load(open("./data.yml"))["sub"]) def test_sub(self, a, b, expect): result1 = self.calc.sub(a, b) assert result1 == expect @pytest.mark.parametrize("a,b,expect", yaml.safe_load(open("./data.yml"))["mul"]) def test_mul(self, a, b, expect): result2 = self.calc.mul(a, b) assert result2 == expect @pytest.mark.parametrize("a,b,expect", yaml.safe_load(open("./data.yml"))["div"]) def test_div(self, a, b, expect): result3 = self.calc.div(a, b) assert result3 == expect
class TestCalculator: # def __init__(self): # pass def setup_class(self): self.cal = Calculator() def test_o(self, get_data): print(f"{get_data[1]}") @allure.story("测试加法功能") @pytest.mark.run(order=4) @pytest.mark.parametrize("a, b, excepted", get_data()[0]) def test_add(self, a, b, excepted, print_mark): assert self.cal.add(a, b) == excepted @allure.story("测试减法功能") @pytest.mark.run(order=3) @pytest.mark.parametrize("a, b, excepted", get_data()[1]) def test_sub(self, a, b, excepted, print_mark): res = self.cal.sub(a, b) if isinstance(res, float): res = round(res, 2) assert res == excepted @allure.story("测试乘法功能") @allure.link("http://www.baidu.com", name="testcase") @pytest.mark.run(order=2) @pytest.mark.parametrize("a, b, excepted", get_data()[2]) def test_mul(self, a, b, excepted, print_mark): assert self.cal.mul(a, b) == excepted @allure.story("测试除法功能") @pytest.mark.run(order=1) @pytest.mark.parametrize("a, b, excepted", get_data()[3]) def test_div(self, a, b, excepted, print_mark): with allure.step("step1:第一步"): print("测试allurestep") assert self.cal.div(a, b) == excepted
class TestCalc: def setup_class(self): self.calc = Calculator() @pytest.mark.parametrize("a,b,expect",get_yaml_data()[0]) def test_add(self,a,b,expect): result = self.calc.add(a,b) assert result == expect @pytest.mark.parametrize("a,b,expect", get_yaml_data()[0]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", get_yaml_data()[0]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect @pytest.mark.parametrize("a,b,expect", get_yaml_data()[0]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect