class TestCalc: def setup(self): self.calc = Calc() # 参数化,加法 @pytest.mark.parametrize(("a", "b", "expect"), yaml.safe_load(open("./add_data.yaml"))) def test_add_all(self, a, b, expect): # print(a + b) result = self.calc.add(a, b) print(result) assert expect == result # 除法 @pytest.mark.parametrize(("a", "b", "expect"), yaml.safe_load(open("./div_data.yaml"))) def test_div_all(self, a, b, expect): try: result = self.calc.div(a, b) print(result) except ZeroDivisionError: result = 'None' assert expect == result if __name__ == '__main__': pytest.main(['-vs', 'test_calc.py::TestCalc'])
class TestCalc: # 调用之后被执行一次 @classmethod def setup_class(cls): print("setup_class") # setup_method setup teardown teardown_method每次执行test都会被执行 def setup_method(self): print("setup_method") def setup(self): print("setup") self.Calc = Calc() def teardown(self): print("teardown") def teardown_method(self): print("teardown method") @pytest.mark.parametrize('result, a, b', [(2, 0, 2), (3, 1, 2), (0, -1, 1)]) #order需要与zadd一起使用才能生效 # @pytest.mark.run(order=-1) def test_zaddOne(self, result, a, b): # assert self.Calc.add(0, 2) == 2 assert self.Calc.add(a, b) == result
class TestCalc: def setup(self): self.calc = Calc() def test_add_1(self): # self.calc = Calc() result = self.calc.add(1, 2) print(result) assert 3 == result def test_div_1(self): result = self.calc.div(2, 2) print(result) assert 1 == result if __name__ == '__main__': pytest.main(['-vs', 'pytest_calc.py::TestCalc::test_div_1'])
class TestCalc: def setup(self): self.c = Calc() @pytest.mark.parametrize("a,b,expect", [(1, 2, 3), (0, 0, 0), (-1, 1, 0), (0.2, 2, 2.2)]) def test_add(self, a, b, expect): res = self.c.add(a, b) assert res == expect
class TestCalc: with open("./test_pytest.yaml") as f: data = yaml.load(f) with open("./test_pytest_step.yaml") as f: step = yaml.load(f) def setup(self): print("setup") self.Calc = Calc() @pytest.mark.parametrize('result, a, b', data) #加法 # @pytest.mark.run(order=-1) def test_addOne(self, result, a, b): # assert self.Calc.add(0, 2) == 2 assert self.Calc.add(a, b) == result # @pytest.mark.run(order=1) # def test_addTwo(self): # assert self.Calc.add(1, 2) == 3 @pytest.mark.parametrize('divResult, c, d', [ [0.5, 1, 2], [2, 2, 1], [2, 0.2, 0.1] ]) #除法 def test_divOne(self, divResult, c, d): assert self.Calc.div(c, d) == divResult def test_divTwo(self): with pytest.raises(ZeroDivisionError): self.Calc.div(2, 0) def steps(self, data): step = self.step for step in step: if step == "add": self.Calc.add(data) elif step == "divTwo": with pytest.raises(ZeroDivisionError): self.Calc.div(2, 0)
class TestFixture: def setup(self): print("setup") self.Calc = Calc() def test_add(self): assert 1 + 1 == 2 def test_add2(self, data): assert data[0] + data[1] == data[2] with open("./test_pytest.yaml") as f: data2 = yaml.load(f) @pytest.mark.parametrize('result, a, b', data2) def test_add3(self, result, a, b): assert self.Calc.add(a, b) == result
def setup(self): self.c = Calc()
def test_add_1(self): self.calc = Calc() result = self.calc.add(1, 2) print(result) self.assertEqual(3, result)
class TestCal(unittest.TestCase): def test_add_1(self): self.calc = Calc() result = self.calc.add(1, 2) print(result) self.assertEqual(3, result)
def setup(self): print("setup") self.Calc = Calc()