Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automate unit testing in Python Unittest

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to automate unit testing in Python Unittest. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Python testing framework (this article only deals with PyUnit)

Reference address

2. Environmental preparation

First make sure that Python is installed, and then integrate PyUnit by installing a newer version of PyUnit,Python (PyUnit provides a graphical test interface UnittestGUI.py)

Reference to: view addr

3. Code example

The IDE used is the PyCharm,DEMO structure as shown in the figure

1. A simple example

# Test002_Fail.py#-*-coding:utf-8-*-import unittestclass Test002_Fail (unittest.TestCase): # execute def setUp before the test case (self): execute def tearDown after the print 'Case Before' pass # test case (self): print' Case After' pass # test case 1 def test_Case1 (self): a = 3b = 2 self.assertEqual 'Result Fail') # Test case 2 def test_Case2 (self): a = 2 b = 3 self.assertEqual (if name bMagazine 7 authoritative result Fail') if name = =' main': unittest.main ()

two。 When there are many case, you can use Suite to manage multiple case,suite. It can be regarded as a container of case, which can hold a lot of case.

# SuiteDemo.py#-*-coding:utf-8-*-# A class Add test suite import unittestclass SuiteDemo (unittest.TestCase): # execute def setUp (self) before the test case: print 'Case Before' pass # execute def tearDown (self): print' Case After' pass def test_Case1 (self) after the test case: a = 3b = 2 self.assertEqual) print 'Case1' def test_Case2 (self): a = 2b = 3 self.assertEqual The 'Result Fail') print' Case2'# defines a test set It is convenient to add Casedef suite (): suiteTest = unittest.TestSuite () suiteTest.addTest (SuiteDemo ("test_Case1")) suiteTest.addTest (SuiteDemo ("test_Case2")) return suiteTest# runs if name = = 'main': unittest.main (defaultTest='suite') via Suite by default

3.Suite and Case can be written separately in different Python files, which makes it easy to distinguish between case module and Suite management module. When testing, you only need to add case to the case module, and then add it to the Suite module.

Case module

# TestCaseDemo#-*-coding:utf-8-*-import unittestclass TestCaseDemo (unittest.TestCase): def setUp (self): pass def tearDown (self): pass def test_Case1 (self): print 'test_Case1' def test_Case2 (self): print' test_Case2' def test_Case3 (self): print 'test_Case3' def test_Case4 (self): print' test_Case4'

Next, there are three Suite modules, all of which import the Case module first, then add Suite, and then add Case to the Suite. Finally, you need to use TestRunner for execution, which is equivalent to the executor of Suite.

Simply TestSuite:

# TestCase_Demo1.py#-*-coding:utf-8-*-# Collection writes a method, main calls and starts import unittestfrom TestCase_Demo import TestCaseDemo# to add a test collection, and adds Casedef suite (): suiteTest = unittest.TestSuite () suiteTest.addTest (TestCaseDemo ('test_Case1')) return suiteTest# specifies and starts the test collection, and runs the set method if name =' main': runner = unittest.TextTestRunner () runner.run (suite ())

You can also add Case directly to the main method without defining the Suite method, and then execute

# TestCase_Demo2.py#-*-coding:utf-8-*-# Collection can be written in mainimport unittestfrom TestCase_Demo import TestCaseDemo# and start the test collection if name = = 'main': # add test collection Case, and start suiteTest = unittest.TestSuite () suiteTest.addTest (TestCaseDemo (' test_Case1')) suiteTest.addTest (TestCaseDemo ('test_Case2')) # directly start the collection runner = unittest.TextTestRunner () runner.run (suiteTest)

Different Case can be added to different Suite. Similarly, Suite can be grouped to distinguish different Case, and Suite can also contain

# TestCase_Demo3.py#-*-coding:utf-8-*-# contains collections Multiple sets import unittestfrom TestCase_Demo import TestCaseDemo# add different collections def Suite1 (): suiteTest = unittest.TestSuite () suiteTest.addTest (TestCaseDemo ("test_Case1")) suiteTest.addTest (TestCaseDemo ("test_Case2")) print 'Suite1 run' return suiteTestdef Suite2 (): suiteTest = unittest.TestSuite () suiteTest.addTest (TestCaseDemo ("test_Case3") suiteTest.addTest (TestCaseDemo ("test_Case4")) print 'Suite2 run' return suiteTest# Contains all Suitedef AllSuite (): allTest = unittest.TestSuite ((Suite1 ()) Suite2 ()) return allTest# runtime You can run different Suite or all of them according to different requirements, so it is convenient to manage caseif name = 'main': runner = unittest.TextTestRunner () runner.run (AllSuite ()) for each run.

4. Summary

PyUnit is a unit testing framework of Python, which can be used to manage automated case.

In Python, every method whose name begins with test_ is regarded as a use case and can be seen as a case. Here we introduce how to use Suite to manage Case and run Case in groups, as well as many other functions. Please refer to: unittest-Unit testing framework

This is the end of the unit testing on how to automate Python Unittest. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report