class TestCalc: def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize('a,b,expect', [[1, 1, 2], [231312322, 100, 231312422], [0.1, 0.1, 0.2], [-1, -1, -2]]) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect print(result) @pytest.mark.parametrize('a,b,expect', [[0.4, 0.5, 0.9], [2.7, 1.1, 3.8]]) def test_add_float(self, a, b, expect): result = self.calc.add(a, b) assert round(result, 2) == expect @pytest.mark.parametrize('a,b', [[100, 0], [-22, 0], [0.4, 0]]) def test_div(self, a, b): with pytest.raises(ZeroDivisionError): self.calc.div(a, b) @pytest.mark.parametrize('a,b,expect', [[0, 100, -100], [-1, 1, -2], [-2, -2, -0]]) def test_div(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect print(result)
class TestCalc: def setup_class(self): print('开始计算') self.calc = Calculator() def teardown_class(self): print('计算结束') @pytest.mark.parametrize('a,b,expect', [[1, 1, 2], [-1, -1, -2], [0, -1, -1], [33333333, 1232244353, 1265577686]]) 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.2, 0.3], [1.1, 2.4, 3.5], [-123.3, -234.5, -357.8]]) def test_add_float(self, a, b, expect): result = self.calc.add(a, b) assert round(result, 2) == expect @pytest.mark.parametrize( 'a, b, expect', [[4, 2, 2], [0.1, 0.1, 1], [2.3, 2, 1.15], [-123.3, 4, -30.83]]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert round(result, 2) == expect @pytest.mark.parametrize('a, b', [[2.3, 0], [-123.3, 0], [5, 0]]) def test_div_zero(self, a, b): with pytest.raises(ZeroDivisionError): 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, 100, 200], [0.1, 0.1, 0.2] # ], ids=['int_case', 'bignum_case', 'float_case']) @pytest.mark.parametrize('a, b, expect', get_datas()[0], ids=get_datas()[1]) def test_add(self, a, b, expect): # calc = Calculator() result = self.calc.add(a, b) assert result == expect def test_add1(self): test_data = [[1, 1, 2], [100, 100, 200], [0.1, 0.1, 0.2]] for i in range(0, len(test_data)): # calc = Calculator() result = self.calc.add(test_data[i][0], test_data[i][1]) assert result == test_data[i][2] @pytest.mark.parametrize('a, b', [[1, 0], [10, 0]]) def test_div(self, a, b): with pytest.raises(ZeroDivisionError): self.calc.div(a, b)
class TestCalculator: def setup_class(self): self.cal = Calculator() def setup(self): print("开始计算") def teardown(self): print("计算结束") @pytest.mark.parametrize("a,b,expect", [(1, 2, 3), (4, 5, 9)]) def test_add_int(self, a, b, expect): result = self.cal.add(a, b) assert (result) == expect @pytest.mark.parametrize("a,b,expect", [(0.1, 0.2, 0.3), (0.11, 0.22, 0.33)]) def test_add_float(self, a, b, expect): result = self.cal.add(a, b) assert (round(result, 2)) == expect @pytest.mark.parametrize("a,b,expect", [(1, 2, 0.5), (4, 2, 2)]) def test_div_formal(self, a, b, expect): result = self.cal.div(a, b) assert (result) == expect @pytest.mark.parametrize("a,b", [(1, 0), (4, 0)]) 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 TestCalc: def setup_class(self): print("开始计算") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.run(order=1) #改变执行顺序:加-减-乘-除 # 1-3)-1:加法:测试数据的数据驱动的测试用例 @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.parametrize('a,b,expect', [[0.1, 0.1, 0.2], [0.1, 0.2, 0.3]]) @pytest.mark.run(order=2) def test_add_float(self, a, b, expect): result = self.calc.add(a, b) assert round(result, 2) == expect @pytest.mark.run(order=-2) # 1-3)-2:除法:测试数据的数据驱动的测试用例 @pytest.mark.parametrize('a,b,expect', get_datas()[6], ids=get_datas()[7]) def test_div(self, a, b, expect): result = self.calc.div(a, b) assert result == expect @pytest.mark.run(order=-1) def test_div_zero(self): with pytest.raises(ZeroDivisionError): result = self.calc.div(1, 0) @pytest.mark.run(order=3) # 1-3)-3:减法:测试数据的数据驱动的测试用例 @pytest.mark.parametrize('a,b,expect', get_datas()[2], ids=get_datas()[3]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) assert result == expect @pytest.mark.run(order=4) # 1-3)-4:乘法:测试数据的数据驱动的测试用例 @pytest.mark.parametrize('a,b,expect', get_datas()[4], ids=get_datas()[5]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) assert result == expect @pytest.mark.run(order=5) @pytest.mark.parametrize('a,b,expect', [[0.3, 0.1, 0.03], [0.9, 0.99, 0.891]]) def test_mul_float(self, a, b, expect): result = self.calc.mul(a, b) assert round(result, 3) == expect
class TestAdd: def setup_class(self): self.cal = Calculator() @pytest.mark.usefixtures("begin") @pytest.mark.parametrize("a,b,expect", get_data()["add_data"]["int"]) def test_add_int(self, a, b, expect): result = self.cal.add(a, b) print(a, b) assert (result) == expect @pytest.mark.parametrize("a,b,expect", get_data()["add_data"]["float"]) def test_add_float(self, a, b, expect): result = self.cal.add(a, b) assert (round(result, 2)) == expect
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) @pytest.mark.parametrize("a,b,expect", [(10, 1, 9), (0, 0, 0), (-1, -1, 0)], ids=["int", "minus", "bigint"]) def test_sub(self, a, b, expect): assert expect == self.calc.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(0.1, 1000, 100), (100, 100, 10000), (-1, -1, 1)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.calc.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(10, 4, 2.5), (0, 100, 0), (1000, 1, 1000)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.calc.div(a, b)
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, 300, 400)], ids=["int", "minus", "bigint"]) def test_add(self, a, b, expect): assert expect == self.cal.add(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, -2), (-1, -2, 1), (100, 300, -200)], ids=["int", "minus", "bigint"]) def test_sub(self, a, b, expect): assert expect == self.cal.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, 15), (-1, -2, 2), (100, 300, 30000)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.cal.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, 0.6), (-1, -2, 0.5), (100, 50, 2.0)], ids=["int", "minus", "bigint"]) def test_mod(self, a, b, expect): assert expect == self.cal.mod(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): 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", [(2, 5, 10), (2.5, 4, 10), (100, 200, 20000)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.calc.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(10, 2, 5), (6.2, 2, 3.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('setup_class') def teardown_class(self): print('teardown_class') def setup(self): print('计算开始') def teardown(self): print('计算结束') @pytest.mark.parametrize('a,b,c', [(1, 1, 2), (200, 200, 400), (0.1, 0.1, 0.2)], ids=['xiao', 'da', 'fudianshu']) def test_add(self, a, b, c): # calc = Calculator() result = self.calc.add(a, b) assert result == c @pytest.mark.parametrize('a,b,c', [(1, 1, 0), (200, 200, 0), (300, 100, 200)], ids=('1-1', '200-200', '300-100')) def test_sub(self, a, b, c): result = self.calc.sub(a, b) assert result == c
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 TestCalc: def setup_class(self): print("计算开始") self.calc = Calculator() def teardown_class(self): print("计算结束") @pytest.mark.parametrize('a,b,expect', get_datas()[0], ids=get_datas()[1]) def test_add(self, a, b, expect): # calc = Calculator() result = self.calc.add(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 def test_add_steps(self): a = 1 b = 1 expect = 2 steps("./steps/add_steps.yml", self.calc, a, b, expect)
class TestCalc: #self.cal = Calculator()在类下面的方法中都要用到,如果每个方法里面都写入这一句会很麻烦,所以在类的开始只执行一次,然后在方法里面调用即可 def setup_class(self): self.cal = Calculator() print("开始计算") def teardown_class(self): print("结束计算") def setup_method(self): print("setup:开始执行测试用例") def teardown_method(self): print("teardown:结束执行测试用例") @pytest.mark.parametrize("a,b,expect",[(3,5,8),(-1,-2,-3),(100,300,400)],ids=["interesting","minus","bigint"]) def test_add(self,a,b,expect): assert expect == self.cal.add(a,b) @pytest.mark.parametrize("a,b,expect",[(6,3,3),(-8,-2,-6),(300,100,200)],ids=["interesting","minus","bigint"]) def test_sub(self, a, b, expect): assert expect == self.cal.sub(a,b) @pytest.mark.parametrize("a,b,expect", [(3,2,6), (-2,-5,10), (100,100,10000)],ids=["interesting", "minus", "bigint"]) def test_mul(self,a,b,expect): assert expect == self.cal.mul(a,b) @pytest.mark.parametrize("a,b,expect", [(10,2,5), (-8, -2, 4), (300, 100, 3)],ids=["interesting", "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 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 TestParam(): def setup_class(self): self.calc = Calculator() @pytest.mark.parametrize("a, b, expect", yaml.safe_load(open("./data.yml"))["datas"]) def test_add(self, a, b, expect): assert self.calc.add(a, b) == 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 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.calc = Calculator() def setup_method(self): print("开始计算") def teardown_method(self): print("结束计算") @pytest.mark.parametrize("a,b,expect", [(3, 5, 8), (-1, -2, -3), (1000, 1000, 2000)], ids=["int", "minus", "bigint"]) def test_add(self, a, b, expect): assert self.calc.add(a, b) == expect @pytest.mark.parametrize("a,b,expect", [(5, 3, 2), (-3, -1, -2), (1000, 800, 200)], ids=["int", "minus", "bigint"]) def test_sub(self, a, b, expect): assert self.calc.sub(a, b) == expect @pytest.mark.parametrize("a,b,expect", [(1, 2, 2), (-5, -6, 30), (2000, 3000, 6000000)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert self.calc.mul(a, b) == expect @pytest.mark.parametrize("a, b, expect", [(6, 2, 3), (-50, -5, 10), (10000, 1000, 10)], ids=["int", "minus", "bigint"]) def test_divide(self, a, b, expect): assert self.calc.divide(a, b) == expect
class TestCalc: # 因为生成实例是下面方法都需要生成的,可以抽离出来setup,使得代码不冗余 def setup_class(self): self.cal = Calculator() print("开始计算") def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,expected", yaml.safe_load(open("./homework2.yml"))["add"]) def test_add(self, a, b, expected): assert expected == self.cal.add(a, b) # @pytest.mark.parametrize("a,b,expect", [(3, 5, -2), (99, 44, 55)], ids=["sub1", "sub2"]) @pytest.mark.parametrize("a,b,expected", yaml.safe_load(open("./homework2.yml"))["sub"], ids=yaml.safe_load(open("./homework2.yml"))["sub_name"]) def test_sub(self, a, b, expect): assert expect == self.cal.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, 15), (99, 2, 198)], ids=["mul1", "mul2"]) def test_mul(self, a, b, expect): assert expect == self.cal.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(10, 5, 2), (99, 2, 49.5)], ids=["div1", "div2"]) def test_div(self, a, b, expect): assert expect == self.cal.div(a, b)
class TestCalc: def setup(self): print("开始计算") def teardown(self): print("关闭计算") def setup_class(self): self.clc = Calculator() @pytest.mark.parametrize("a,b,hopevalue",get_datas("add_datas"),ids=get_datas("myids")) def test_add(self,a,b,hopevalue): #加法测试 result = self.clc.add(a,b) assert result == hopevalue @pytest.mark.parametrize("a,b,hopevalue",get_datas("sub_datas"),ids=get_datas("myids")) def test_sub(self,a,b,hopevalue): #减法测试 result = self.clc.sub(a,b) assert result == hopevalue @pytest.mark.parametrize("a,b,hopevalue", get_datas("mul_datas"), ids=get_datas("myids")) def test_mul(self, a, b, hopevalue): #乘法测试 result = self.clc.mul(a, b) assert result == hopevalue @pytest.mark.parametrize("a,b,hopevalue", get_datas("div_datas"), ids=get_datas("myids")) def test_div(self, a, b, hopevalue): #除法测试 result = self.clc.div(a, b) assert result == hopevalue
class TestCalc: # 因为生成实例是下面方法都需要生成的,可以抽离出来setup,使得代码不冗余 def setup_class(self): self.cal = Calculator() print("开始计算") def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,expected", [(3, 5, 8), (-1, -2, -3), (1000, 1000, 2000)], ids=["中文add1", "add2", "add3"]) def test_add(self, a, b, expected): assert expected == self.cal.add(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, -2), (99, 44, 55)], ids=["sub1", "sub2"]) def test_sub(self, a, b, expect): assert expect == self.cal.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(3, 5, 15), (99, 2, 198)], ids=["mul1", "mul2"]) def test_mul(self, a, b, expect): assert expect == self.cal.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(10, 5, 2), (99, 2, 49.5)], ids=["div1", "div2"]) def test_div(self, a, b, expect): assert expect == self.cal.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 TestCal: def setup_class(self): print("\n开始计算") # 实例化计算器的类 self.cal = Calculator() def teardown_class(self): print("\n结束计算") @pytest.mark.parametrize('a,b,exp', datas['add'], ids=ids['add']) def test_add(self, a, b, exp): res = self.cal.add(a, b) assert res == exp @pytest.mark.parametrize('a,b,exp', datas['sub'], ids=ids['sub']) def test_sub(self, a, b, exp): res = self.cal.sub(a, b) assert res == exp @pytest.mark.parametrize('a,b,exp', datas['mul'], ids=ids['mul']) def test_mul(self, a, b, exp): res = self.cal.mul(a, b) assert res == exp @pytest.mark.parametrize('a,b,exp', datas['div'], ids=ids['div']) def test_div(self, a, b, exp): res = self.cal.div(a, b) assert res == exp
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 TestCalc(): def setup_class(self): self.calc = Calculator() print("开始计算") def teardown_class(self): print("结束计算") @pytest.mark.parametrize("a,b,expect", get_datas()[0], ids=get_datas()[1]) def test_add(self, a, b, expect, myfixture): assert expect == self.calc.add(a, b) @pytest.mark.parametrize("a,b,expect", [(1, 1, 0), (-3, -2, -1), (1000, 2000, -1000)], ids=["int", "minus", "bigint"]) def test_sub(self, a, b, expect): assert expect == self.calc.sub(a, b) @pytest.mark.parametrize("a,b,expect", [(1, 1, 1), (-3, -2, 6), (1000, 2, 2000)], ids=["int", "minus", "bigint"]) def test_mul(self, a, b, expect): assert expect == self.calc.mul(a, b) @pytest.mark.parametrize("a,b,expect", [(1, 1, 1), (-6, -3, 2), (1000, 500, 2)], ids=["int", "minus", "bigint"]) def test_div(self, a, b, expect, myfixture): # self.calc = Calculator() 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 TestCalc: def setup_class(self): print("开始计算") self.calc = Calculator() def teardown_class(self): print("计算结束") #装饰器@pytest.mark.parametrize() @pytest.mark.parametrize( 'a,b,expect', [[1, 1, 2], [100000, 100000, 200000], [0.1, 0.1, 0.2], [-1, -1, -2], [1, 0, 1], [1, -1, 0]], ids=[ 'int_case', 'bignum_case', 'float_case', 'minus_case', 'zero_case', 'mix_case' ]) def test_add(self, a, b, expect): result = self.calc.add(a, b) assert result == expect @pytest.mark.parametrize( 'a,b,expect', [[2, 1, 2], [1, 2, 0.5], [-3, -1, 3], [-1, 4, -0.25], [0, 1, 0]], ids=['int_case', 'float_case', 'minus_case', 'mix_case', 'zero_case']) def test_div(self, a, b, expect): result = self.calc.div(a, b) def test_zero_div(self): with pytest.raises(ZeroDivisionError): 1 / 0
class TestCalc: def setup_method(self): print("\nsetup_method:调用方法开始计算") def teardown_method(self): print("\nteardown_method:方法调用完成计算结束") def setup_class(self): self.calc = Calculator() @pytest.mark.parametrize("a,b,expect", get_datas()["add"]) def test_add(self, a, b, expect): result = self.calc.add(a, b) print(result) assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()["sub"]) def test_sub(self, a, b, expect): result = self.calc.sub(a, b) print(result) assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()["mul"]) def test_mul(self, a, b, expect): result = self.calc.mul(a, b) print(result) assert result == expect @pytest.mark.parametrize("a,b,expect", get_datas()["div"]) def test_div(self, a, b, expect): result = self.calc.div(a, b) print(result) assert result == expect