unittest运行测试的三种方式:

  • unittest.main()
  • TestSuit/TextTestRunner
  • discover()/ TextTestRunner

unittest.main()会执行所有用例

TestSuit/TextTestRunner

灵活测试某些用例

  1. import unittest
  2. class MyTest(unittest.TestCase):
  3. def test_case(self):
  4. print("test 2 + 2 ")
  5. self.assertEqual(2 + 2, 4)
  6. class MyTest2(unittest.TestCase):
  7. def test_case(self):
  8. print("test 2 - 2 ")
  9. self.assertEqual(2 - 2, 0)
  10. if __name__ == '__main__':
  11. # 测试套件
  12. suit = unittest.TestSuite()
  13. suit.addTest(MyTest("test_case"))
  14. suit.addTest(MyTest2("test_case"))
  15. # 运行测试套件
  16. runner = unittest.TextTestRunner()
  17. runner.run(suit)

discover()/ TextTestRunner

  1. import unittest
  2. suit = unittest.defaultTestLoader.discover("./", "unittest_*.py") # 执行所有unittest_开头的文件
  3. # 运行测试套件
  4. runner = unittest.TextTestRunner()
  5. runner.run(suit)
  1. # unittest_assert.py
  2. import unittest
  3. class MyClass(unittest.TestCase):
  4. def test_equal(self):
  5. user = "admin"
  6. self.assertEqual(user, "admin")
  7. self.assertNotEqual("a", "b")
  8. def test_in(self):
  9. info = "恭喜,你的邮件已经发送成功!"
  10. self.assertIn("成功", info)
  11. self.assertNotIn("失败", info)
  12. def test_true(self):
  13. a = False
  14. b = True
  15. self.assertTrue(b)
  16. self.assertFalse(a)
  17. if __name__ == '__main__':
  18. unittest.main()
  1. # unittest_demo.py
  2. import unittest
  3. class MyClass(unittest.TestCase):
  4. def test_equal(self):
  5. user = "admin"
  6. self.assertEqual(user, "admin")
  7. self.assertNotEqual("a", "b")
  8. def test_in(self):
  9. info = "恭喜,你的邮件已经发送成功!"
  10. self.assertIn("成功", info)
  11. self.assertNotIn("失败", info)
  12. def test_true(self):
  13. a = False
  14. b = True
  15. self.assertTrue(b)
  16. self.assertFalse(a)
  17. if __name__ == '__main__':
  18. unittest.main()
  1. # unittest_fixture.py
  2. import unittest
  3. def setUpModule():
  4. print(">>>>>>>>>>>>>> start module")
  5. def tearDownModule():
  6. print(">>>>>>>>>>>>>> start module")
  7. class MyClass(unittest.TestCase):
  8. @classmethod
  9. def setUpClass(cls):
  10. print("====> start class")
  11. @classmethod
  12. def tearDownClass(cls):
  13. print("===> end class")
  14. def setUp(self):
  15. print("start test")
  16. def tearDown(self):
  17. print("end test")
  18. def test_case(self):
  19. print("---->test case")
  20. self.assertEqual(2+2, 4)
  21. def test_case2(self):
  22. print("---->test case2")
  23. self.assertEqual(3, 4)
  24. class MyClass2(unittest.TestCase):
  25. def test_case3(self):
  26. print("---> test case3")
  27. self.assertEqual(3, 3)
  28. if __name__ == '__main__':
  29. unittest.main()
  1. # unittest_runner.py
  2. import unittest
  3. class MyTest(unittest.TestCase):
  4. def test_case(self):
  5. print("test 2 + 2 ")
  6. self.assertEqual(2 + 2, 4)
  7. class MyTest2(unittest.TestCase):
  8. def test_case(self):
  9. print("test 2 - 2 ")
  10. self.assertEqual(2 - 2, 1)
  11. if __name__ == '__main__':
  12. # unittest.main()
  13. # 测试套件
  14. suit = unittest.TestSuite()
  15. # suit.addTest(MyTest2("test_case"))
  16. suit.addTest(MyTest("test_case"))
  17. # 运行测试套件
  18. runner = unittest.TextTestRunner()
  19. runner.run(suit)