unittest常用断言方法

  • assertEqual(a, b)/assertNotEqual(a, b)
  • assertln(a, b)/assertNotln(a, b)
  • assertTrue(x)/assertFalse(x)
  1. import unittest
  2. class MyClass(unittest.TestCase):
  3. def test_equal(self):
  4. user = "admin"
  5. self.assertEqual(user, "admin")
  6. self.assertNotEqual("a", "b")
  7. def test_in(self):
  8. info = "恭喜,你的邮件已经发送成功!"
  9. self.assertIn("成功", info)
  10. self.assertNotIn("失败", info)
  11. def test_true(self):
  12. a = False
  13. b = True
  14. self.assertTrue(b)
  15. self.assertFalse(a)
  16. if __name__ == '__main__':
  17. unittest.main()