当前位置:网站首页>Unittest use

Unittest use

2022-06-21 07:07:00 sl01224318

Catalog

Introduce

Conceptual analysis

TestCase

TestSuite

TestRunner

TestLoader

Test fixture

Introduce

        unittest yes python Unit test framework , Be similar to Junit frame , Among them is 5 A more important concept , Let's share with you .

        unittest Medium 5 Usage are test fixture,test case,test suite,test runner,test loader, Mastering these methods can better help us use unittest.

Conceptual analysis

TestCase

         One TestCase The example of is a test case . A test case includes the setup of the pre test environment (setup), Execute test code , And the restoration of the environment after testing , unit testing (unittest) And that's the essence of it , A test case is a complete test unit , By running this test unit , You can verify a problem . for example :

class MyTestCase03(unittest.TestCase):  # quote unittest.Testcase
 
    def test01(self):
        print('test01')

    def test02(self):
        print('test02')

TestSuite

         Multiple test cases come together , Namely TestSuite, It can also be understood as a collection of multiple test cases , and TestSuite You can also nest TestSuite.

TestRunner

        TestRunner Is used to execute test cases , Among them run(test) Will execute TestSuite/TestCase Medium run(result) Method , for example :

class MyTestCase04(unittest.TestCase):
    def test03(self):
        print('test03')
    def test04(self):
        print('test04')

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite)     # Use run() Method to execute the test case 

TestLoader

        TestLoader It's used to load TestCase To TestSuite Medium , Some of them loadTestFrom Method , It's looking for it from all over the place TestCase, Create instances of them , then add To TestSuite in , Return to one more TserSuite example .

class MyTestCase04(unittest.TestCase):
    def test03(self):
        print('test03')
    def test04(self):
        print('test04')


if __name__ == '__main__':
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    suite.addTest(loader.loadTestsFromTestCase(MyTestCase04))  # Load test cases 

Test fixture

        Test Fixture Used before the test method , Or after the test method , The main function is to provide some devices for testing , These devices can be data , It can be an environment configuration or a pre run state , By covering TestCase() Of setUp() and tearDown() Method to implement , For example, we define a before the test case setup、teardown, In this way, each use case will be executed first setup、tewrdown Method .

class MyTestCase(unittest.TestCase):

   
    def setUp(self) -> None:
        print('setup...')

    def tearDown(self) -> None:
        print('teardown...')

    def test01(self):
        print('test01')

    def test02(self):
        print('test02')

if __name__ == '__main__':
    unittest.main()

 

原网站

版权声明
本文为[sl01224318]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210647394086.html