6 Python unittest-TestCase 运行模式

2019-08-14  本文已影响0人  降水

运行模式 主要应用于三个基础:TestSuite, Testcase, Test


准备测试目录文件:

     test1
        -- test_a.py
        -- test_b.py
     test2
        -- test_c.py
        -- test_d.py
     -- main.py

test_a.py( 其他测试文件类似)

import unittest
class TestA(unittest.TestCase):
  def test_1(self):
      self.assertIs(1,1)
  def test_2(self):
      self.assertIs(1,1)
if __name__ == "__main__":
  unittest.main()

main.py

import unittest, test1.main1, test2.main2,
if __name__ == "__main__":
   suite = unittest.TestSuite()
   suite.addTest(suite1)
   suite.addTest(suite2)
   runner = unittest.TextTestRunner()
   runner.run(suite)

1 单个Test
   runner = unittest.TextTestRunner()
   case = TestA('test_1')
   runner.run(case)
2 多个Test
   suite = unittest.TestSuite() 
   suite.addTest(TestA('test_1')) 
   suite.addTest(TestB('test_1')) 
   runner = unittest.TextTestRunner()
   runner.run(suite)
3 单个TestCase
   runner = unittest.TextTestRunner()
   runner.run(TestA)
4 多个TestCase
   suite = unittest.TestSuite()
   all_cases = unittest.defaultTestLoader.discover('./test', pattern='test*.py')
   for case in all_cases:
       suite.addTests(case)
   runner = unittest.TextTestRunner()
   runner.run(suite)
5 单个TestSuite,使用如上2,4
6 多个TestSuite
   # 已得到suite1,suite2
   suite = unittest.TestSuite()
   suite.addTest(suite1)
   suite.addTest(suite2)
   runner = unittest.TextTestRunner()
   runner.run(suite)
7 混合模式,使用TestSuite,添加不同的用例及套件即可,例子不再写了:)
报告结果截图如下

-- Github 地址 https://github.com/mingyuanHub/python-game-test

image.png
上一篇下一篇

猜你喜欢

热点阅读