当前位置:网站首页>TestSuite and testrunner in unittest

TestSuite and testrunner in unittest

2022-07-04 14:09:00 Field test record

1、Unittest Medium main use TestSuite Add test cases to the test suite , Reuse Testrunner Run test cases , And generate test reports .TestSuite You can specify the test cases to run , than main Function running test cases should be flexible .

2、TestSuite and TestRunner usage

unittest_testsuite_testrunner_demo.py
#  Guide pack 
import unittest

#  Create the class of the test case 
class TestSuiteDemo1(unittest.TestCase):
    def test01(self):
        print(" The test case : Daejeon 03")

    def test02(self):
        print(" The test case : Daejeon 04")

class TestSuiteDemo2(unittest.TestCase):
    def test01(self):
        print(" The test case : Daejeon 01")

    def test02(self):
        print(" The test case : Daejeon 02")


if __name__ == '__main__':

    # Run the specified test case method :

     #  Instantiate the test suite 
     suite = unittest.TestSuite()

     #  Add test cases to the test suite 
     suite.addTest(TestSuiteDemo1('test01'))

     #  Instantiation test_runner
     runner = unittest.TextTestRunner()

     #  Use runner Run the test suite 
     runner.run(suite)

Running results : You can see that only the test cases you want to run

 3、TestSuite There are three ways to add test cases

1)suite.addTest: Add a single test case

Just look at the example above

2)suite.addTests: Add multiple test cases

#  Add multiple test cases 
if __name__ == '__main__':

    # Run the specified test case method :

     #  Instantiate the test suite 
     suite = unittest.TestSuite()

     #  Add test cases to the test suite : Add a single test case method 
     # suite.addTest(TestSuiteDemo1('test01'))

     # Add multiple test cases 
     suite.addTests([TestSuiteDemo2('test01'),TestSuiteDemo2('test02')])

     #  Instantiation test_runner
     runner = unittest.TextTestRunner()

     #  Use runner Run the test suite 
     runner.run(suite)

Running results :

 

3)TestLoader

The first one is :TestLoader().discover(("./", "*.py")
Find the specified according to the execution path py Test cases in the file , Load into the test suite
The second kind :TestLoader().loadTestsFromTestCase( Test case class name )
Load the test case according to the class name of the test case
unittest_testloader_demo.py

import unittest
def testXX(aa):
  print(" Extra test cases ")

class TestSuiteDemo1(unittest.TestCase):
 def test01(self):
   print(" Execute test case aaaa")

 def test02(self):
   print(" Execute test case bbbb")

class TestSuiteDemo2(unittest.TestCase):
   def test01(self):
     print(" Execute test case xxxx")
   def test02(self):
     print(" Execute test case yyyy")

class TestSuiteDemo3(unittest.TestCase):
   def test01(self):
     print(" Perform interface tests ")
   def test02(self):
     print(" Perform unit tests ")
   def test03(self):
     print(" Perform safety test ")


if __name__ == '__main__':
  t1 = unittest.TestLoader()
  # Method 1
  suite = t1.discover("./", "*.py") #  Use discover To find test cases 
  # Method 2
  # suite = t1.loadTestsFromTestCase(TestSuiteDemo3)
  runner = unittest.runner.TextTestRunner() #  Instantiation runner
  runner.run(suite) #  Use runner Execute test suite 

原网站

版权声明
本文为[Field test record]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041107208630.html