Test fixture分三类

  • setUp/tearDown
  • setUpClass/tearDownClass
  • setUpModule/tearDownModule

    setUp/tearDown

    函数级,分别在函数的开始和结束进行。

    单个测试用例

    ```python

    用例级别(函数)的测试案例

    import unittest

class MyClass(unittest.TestCase):

  1. def setUp(self):
  2. print("====> start class")
  3. def tearDown(self):
  4. print("===> end class")
  5. def test_case(self):
  6. print("---->test case")

if name == ‘main‘: unittest.main()

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12405790/1630598507372-fab67452-c5a5-44e8-970d-6ef042bf2dab.png#clientId=u0d00ed0a-b184-4&from=paste&height=223&id=ua3fe66ea&margin=%5Bobject%20Object%5D&name=image.png&originHeight=446&originWidth=1285&originalType=binary&ratio=1&size=42338&status=done&style=none&taskId=u754a049f-c9b2-4b5a-a2f4-e72d80c1be7&width=642.5)<br />可以看到各个函数执行的先后顺序。
  2. - [x] setUp
  3. - [x] test_
  4. - [x] tearDown
  5. <a name="aMwdJ"></a>
  6. ## 多个测试用例1
  7. ```python
  8. # 全正确的用例
  9. import unittest
  10. class MyClass(unittest.TestCase):
  11. def setUp(self):
  12. print("====> start class")
  13. def tearDown(self):
  14. print("===> end class")
  15. def test_case2(self):
  16. print("---->test case2")
  17. self.assertEqual(4, 4)
  18. def test_case(self):
  19. print("---->test case")
  20. if __name__ == '__main__':
  21. unittest.main()

image.png

多个测试用例2

  1. # 有错误的用例
  2. import unittest
  3. class MyClass(unittest.TestCase):
  4. def setUp(self):
  5. print("====> start class")
  6. def tearDown(self):
  7. print("===> end class")
  8. def test_case2(self):
  9. print("---->test case2")
  10. self.assertEqual(3, 4)
  11. def test_case(self):
  12. print("---->test case")
  13. if __name__ == '__main__':
  14. unittest.main()

image.png

setUpClass/tearDownClass

类级别,分别在类的开始和结束执行。

  1. import unittest
  2. class MyClass(unittest.TestCase):
  3. @classmethod
  4. def setUpClass(cls):
  5. print("====> start class")
  6. @classmethod
  7. def tearDownClass(cls):
  8. print("===> end class")
  9. def setUp(self):
  10. print("start test")
  11. def tearDown(self):
  12. print("end test")
  13. def test_case(self):
  14. print("---->test case")
  15. self.assertEqual(2 + 2, 4)
  16. def test_case2(self):
  17. print("---->test case2")
  18. self.assertEqual(4, 4)
  19. if __name__ == '__main__':
  20. unittest.main()

image.png

setUpModule/tearDownModule

文件级别,分别在文件的开始和结束执行。

  1. import unittest
  2. def setUpModule():
  3. print(">>>>>>>>>>>>>> start module")
  4. def tearDownModule():
  5. print(">>>>>>>>>>>>>> start module")
  6. class MyClass(unittest.TestCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. print("====> start class")
  10. @classmethod
  11. def tearDownClass(cls):
  12. print("===> end class")
  13. def setUp(self):
  14. print("start test")
  15. def tearDown(self):
  16. print("end test")
  17. def test_case(self):
  18. print("---->test case")
  19. self.assertEqual(2 + 2, 4)
  20. def test_case2(self):
  21. print("---->test case2")
  22. self.assertEqual(4, 4)
  23. if __name__ == '__main__':
  24. unittest.main()

image.png