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(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): 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(): 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) -> 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(TestCase): def setUpClass(cls) -> None: self.calc = Calc() @data(1, 2, 3, 4, 5, 6) def test_demo(self, real): self.assertEqual(real, 5) @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) result = self.calc.add(a, b) # print("result="+str(result)) # assert result == 2 print(a, b, c) self.assertEqual(self.calc.add(a, b), c) @file_data("calc.yaml") def test_div(self, a, b, c): calc = Calc() print(a, b, c) self.assertEqual(calc.div(a, b), c)
def test_above(self): calc = Calc() result = calc.above(1, 1) self.assertFalse(result) result = calc.above(2, 1) self.assertTrue(result) result = calc.above(1, 2) self.assertFalse(result) result = calc.above(-1, -2) self.assertTrue(result) result = calc.above(3.2, 2.2) self.assertTrue(result) result = calc.above(2.2, 3.2) self.assertFalse(result)
class TestCalc(TestCase): def setUp(self): self.calc = Calc() @data(0, 1, 2, 3) def test_demo(self, value): self.assertTrue(value) @data((1, 1, 2), (1, 0, 1), (1, -1, 0), (1, 1000000, 1000001)) @unpack def test_add(self, a, b, c): #self.fail() # fail calc=Calc() # result=calc.add(a,b) #assert result==2 self.assertEqual(self.calc.add(a, b), c) # # @file_data("calc.yaml") # def test_div(self , a,b,c): # calc=Calc() # result=calc(a,b) # self.assertEqual(result,c) def test_above(self): calc = Calc() result = calc.above(1, 1) self.assertFalse(result) result = calc.above(2, 1) self.assertTrue(result) result = calc.above(1, 2) self.assertFalse(result) result = calc.above(-1, -2) self.assertTrue(result) result = calc.above(3.2, 2.2) self.assertTrue(result) result = calc.above(2.2, 3.2) self.assertFalse(result)
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() result = calc.div(a, b) self.assertEqual(result, c)
def test_add_02(): assert Calc(-9, 2).add() == -7
def test_shou_02(): assert Calc(-9, 2).shou() == -4.5
def test_add_01(): assert Calc(9, 2).add() == 11
def test_shou_01(): assert Calc(9, 2).shou() == 4.5
def test_seki_02(): assert Calc(-9, 2).seki() == -18
def setUp(self): self.calc = Calc()
def setup(self) -> None: self.calc = Calc()
def test_div(self, a, b, c): calc = Calc() print(a, b, c) self.assertEqual(calc.div(a, b), c)
def setup(self): self.calc = Calc() pass
def setUpClass(cls) -> None: self.calc = Calc()
def test_above(self, a, b, c): calc = Calc() result = calc.above(a, b) self.assertEqual(result, c)
def test_sum(self): calc = Calc() assert calc.sum(1, 2, 3) == 6 assert calc.sum(1, 1, 1) == 4
def test_dif_01(): assert Calc(9, 2).dif() == 7
def setup_method(self) -> None: self.calc = Calc()
def test_dif_02(): assert Calc(-9, 2).dif() == -11
def setup_class(self): print("setup class only run once") self.calc = Calc()
def test_seki_01(): assert Calc(9, 2).seki() == 18