class TestCalc: def setup_method(self) -> None: self.calc = Calc() @pytest.mark.parametrize("a,b,c", [(1, 1, 2), (1, 2, 3), (2, 2, 4)]) def test_add(self, a, b, c): result = self.calc.add(a, b) assert c == result # @file_data('calc.yaml') # @pytest.mark.parametrize("a,b,c",json.load(open('calc.json'))) @pytest.mark.parametrize("a,b,c", yaml.load( open(os.getcwd() + '/testcase/calc.yaml'))) def test_div(self, a, b, c): # calc = Calc() assert self.calc.div(a, b) == c def test_above(self): # calc = Calc() result = self.calc.above(1, 2) assert False == result result = self.calc.above(2, 2) assert False == result
class TestCalc: def setup(self) -> None: self.calc = Calc() @pytest.mark.parametrize("a,b,c", [(1, 1, 2), (1, 0, 1), (1, -1, 0), (1000000, 1, 1000001)]) def test_add(self, a, b, c): assert self.calc.add(a, b) == c #@file_data("calc.yaml") @pytest.mark.parametrize("a,b,c", yaml.load(open("calc2.yaml"))) def test_div(self, a, b, c): print(a, b, c) assert self.calc.div(a, b) == c @data( (3, 2, True), #验证a>b的 (1, -2, True), (4, 4, False), #验证a=b的 (0, 0, False), (1, 5, False), #验证a<b的 (-1, 0, False), ("a", "b", False), #传入非数值型,验证容错 (3.1, 2, True), #浮点型 (2.2, 2.2, False), (-1.5, -1.1, False)) @unpack def test_above(self, a, b, c): assert self.calc.above(a, b) == c
class TestCalc(): def setup(self) -> None: self.calc = Calc() @pytest.mark.parametrize("a, b, c", [(1, 1, 2), (1, 0, 1), (0.4, 1, 1.4), (1000, 1, 1001), (-1, 1, 0)]) def test_add(self, a, b, c): assert self.calc.add(a, b) == c # @file_data('calc.yaml') @pytest.mark.parametrize("a, b, c", yaml.load(open("calc2.yaml"))) def test_div(self, a, b, c): assert self.calc.div(a, b) == c def test_above(self): result = self.calc.above(100, 1) self.assertEqual(result, True) result = self.calc.above(1, 3) self.assertEqual(result, False) result = self.calc.above(3.3, 3) self.assertEqual(result, True) result = self.calc.above(0, -3) self.assertEqual(result, True) result = self.calc.above(-2, -3) self.assertEqual(result, True) result = self.calc.above(-5, -3) self.assertEqual(result, False)
class TestCalc: def setup(self): self.calc = Calc() pass # @pytest.mark.add @pytest.mark.parametrize("v1, v2, v3", yaml.safe_load(open("../data/add.yml"))) def test_add(self, v1, v2, v3): print(v1, v2, v3) try: result = self.calc.add(v1, v2) assert v3 == pytest.approx(result) except TypeError as e: result = "TypeError" assert v3 == result # @pytest.mark.div @pytest.mark.parametrize("v1, v2, v3", yaml.safe_load(open("../data/div.yml"))) def test_div(self, v1, v2, v3): # self.calc = Calc() try: result = self.calc.div(v1, v2) except Exception as e: result = e.__class__.__name__ assert v3 == result else: assert v3 == pytest.approx(result) # @pytest.mark.sub @pytest.mark.parametrize("v1, v2, v3", yaml.safe_load(open("../data/sub.yml"))) def test_sub(self, v1, v2, v3): try: result = self.calc.sub(v1, v2) except Exception as e: result = e.__class__.__name__ assert v3 == result else: assert v3 == pytest.approx(result) # @pytest.mark.multi @pytest.mark.parametrize("v1, v2, v3", yaml.safe_load(open("../data/multi.yml"))) def test_multi(self, v1, v2, v3): try: result = self.calc.multi(v1, v2) except Exception as e: result = e.__class__.__name__ assert v3 == result else: assert v3 == pytest.approx(result)
class TestCalc(TestCase): def setUp(self) -> None: self.calc = Calc() @data((1, 1, 2), (1, 0, 1), (1, -1, 0), (1, 1000000, 1000001)) @unpack def test_add(self, a, b, c): print(a, b, c) self.assertEqual(self.calc.add(a, b), c) @file_data("calc.yaml") def test_div(self, a, b, c): print(a, b, c) self.assertEqual(self.calc.div(a, b), c)
class TestCalc: def setup(self) -> None: self.calc = Calc() @pytest.mark.parametrize("a, b, c", [(1, 1, 2), (1, 0, 1), (1, -1, 0), (1, 1000000, 1000001)]) def test_add(self, a, b, c): print(a, b, c) assert self.calc.add(a, b) == c # @file_data("calc.yaml") @pytest.mark.parametrize("a,b,c", yaml.load(open("calc2.yaml"))) def test_div(self, a, b, c): print(a, b, c) assert self.calc.div(a, b) == c
class TestCalc(TestCase): def setUp(self) -> None: self.calc = Calc() @data(5) def test_demo(self, real): self.assertEqual(real, 5) @data((1, 1, 2), (1, 0, 1)) @unpack def test_add(self, a, b, c): self.assertEqual(self.calc.add(a, b), c) @file_data("calc.yaml") def test_div(self, a, b, c): self.assertEqual(self.calc.div(a, b), c)
class TestCalcPytest: def setup_class(self): print("setup class only run once") self.calc = Calc() def setup(self): print("setup run before every testcase") @pytest.mark.it @pytest.mark.parametrize('first, second, expect', [[2, 1, 2], [3, 1, 3], [0, 3, 0], [0.3, 0.1, 3]]) def test_div(self, first, second, expect): assert self.calc.div(first, second) == expect @pytest.mark.e2e def test_mul(self): assert self.calc.mul(1, 2) == 2 def teardown(self): print('teardown') def teardown_class(self): print('teardown class')
class TestCalc(unittest.TestCase): def setUp(self) -> None: self.calc = Calc() @data((1, 1, 2), (1, 0, 1), (0.4, 1, 1.4), (1000, 1, 1001), (-1, 1, 0)) @unpack def test_add(self, a, b, c): self.assertEqual(self.calc.add(a, b), c) @file_data('calc.yaml') def test_div(self, a, b, c): result = self.calc.div(a, b) self.assertEqual(result, c) def test_above(self): result = self.calc.above(100, 1) self.assertEqual(result, True) result = self.calc.above(1, 3) self.assertEqual(result, False) result = self.calc.above(3.3, 3) self.assertEqual(result, True) result = self.calc.above(0, -3) self.assertEqual(result, True) result = self.calc.above(-2, -3) self.assertEqual(result, True) result = self.calc.above(-5, -3) self.assertEqual(result, False)
def test_div(self, a, b, c): calc = Calc() print(a, b, c) self.assertEqual(calc.div(a, b), c)
def test_div(self, a, b, c): calc = Calc() result = calc.div(a, b) self.assertEqual(result, c)