目录
测试执行顺序
setUpClass
tearDownClass
setUp
tearDown
@unittest.skip
@unittest.skipIf
测试执行方式:
一:直接使用unittest.main()执行
二:可执行部分测试类和其中的部分方法
三:测试类级执行
四:路径执行:
测试类之前执行(只执行一次)
测试类之后执行(只执行一次)
测试方法之前执行(可执行多次)
测试方法之后执行(可执行多次)
跳过执行测试方法
满足条件可跳过执行测试方法
import unittest
class demo(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("setUpClass")
@classmethod
def tearDownClass(cls) -> None:
print("tearDownClass")
# 测试方法之前执行
def setUp(self) -> None:
print("setup")
# 测试方法之后执行(ps:资源回收,断开数据库连接等)
def tearDown(self) -> None:
print("tearDown")
def test_cast01(self):
print("testcast01")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
def test_cast02(self):
print("testcast02")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
# 跳过测试执行
@unittest.skip
def test_cast03(self):
print("testcast03")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
@unittest.skipIf(1 + 1 == 2, "跳过条件") # 满足条件后跳过执行
def test_cast04(self):
print("testcast04")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
if __name__ == '__main__':
unittest.main()
执行效果:
if __name__ == '__main__':
unittest.main()
案例如上
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(demo2("test_demo1_cast02"))
unittest.TextTestRunner().run(suite)
import unittest
class demo(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("setUpClass")
@classmethod
def tearDownClass(cls) -> None:
print("tearDownClass")
# 测试方法之前执行
def setUp(self) -> None:
print("setup")
# 测试方法之后执行(ps:资源回收,断开数据库连接等)
def tearDown(self) -> None:
print("tearDown")
def test_cast01(self):
print("testcast01")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
def test_cast02(self):
print("testcast02")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
# 跳过测试执行
@unittest.skip
def test_cast03(self):
print("testcast03")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
@unittest.skipIf(1 + 1 == 2, "跳过条件") # 满足条件后跳过执行
def test_cast04(self):
print("testcast04")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
class demo2(unittest.TestCase):
def test_demo1_cast01(self):
print("test_demo2_cast01")
def test_demo1_cast02(self):
print("test_demo2_cast02")
if __name__ == '__main__':
suite = unittest.TestSuite()
# suite.addTest(demo("test_cast01"))
suite.addTest(demo2("test_demo1_cast02"))
unittest.TextTestRunner().run(suite)
执行结果:
if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(demo2) suite1 = unittest.TestLoader().loadTestsFromTestCase(demo1) suiteall = unittest.TestSuite([suite, suite1]) unittest.TextTestRunner().run(suiteall)
import unittest
class demo(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
print("setUpClass")
@classmethod
def tearDownClass(cls) -> None:
print("tearDownClass")
# 测试方法之前执行
def setUp(self) -> None:
print("setup")
# 测试方法之后执行(ps:资源回收,断开数据库连接等)
def tearDown(self) -> None:
print("tearDown")
def test_cast01(self):
print("testcast01")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
def test_cast02(self):
print("testcast02")
self.assertEqual(1, 1, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
# 跳过测试执行
@unittest.skip
def test_cast03(self):
print("testcast03")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
@unittest.skipIf(1 + 1 == 2, "跳过条件") # 满足条件后跳过执行
def test_cast04(self):
print("testcast04")
self.assertEqual(1, 2, "不相等!")
self.assertIn("oh", "oh this", "不包含!")
class demo1(unittest.TestCase):
def test_demo1_cast01(self):
print("test_demo1_cast01")
def test_demo1_cast02(self):
print("test_demo1_cast02")
class demo2(unittest.TestCase):
def test_demo2_cast01(self):
print("test_demo2_cast01")
def test_demo2_cast02(self):
print("test_demo2_cast02")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(demo2)
suite1 = unittest.TestLoader().loadTestsFromTestCase(demo1)
suiteall = unittest.TestSuite([suite, suite1])
unittest.TextTestRunner().run(suiteall)
if __name__ == '__main__': discover = unittest.defaultTestLoader.discover("./", "test*.py") unittest.TextTestRunner().run(discover)
import unittest
class demo1(unittest.TestCase):
def test_demo1_cast01(self):
print("test_demo1_cast01")
def test_demo1_cast02(self):
print("test_demo1_cast02")
class demo2(unittest.TestCase):
def test_demo2_cast01(self):
print("test_demo2_cast01")
def test_demo2_cast02(self):
print("test_demo2_cast02")
class demo3(unittest.TestCase):
def test_demo3_cast01(self):
print("test_demo3_cast01")
def test_demo3_cast02(self):
print("test_demo3_cast02")
if __name__ == '__main__':
discover = unittest.defaultTestLoader.discover("./", "test*.py")
unittest.TextTestRunner().run(discover)
执行结果: