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 manage case in automated testing unitest

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly analyzes the relevant knowledge points of how to carry out automated testing unitest case management, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to manage case in automated testing unitest".

1. Association

During testing, the return value of the first case is often used as the request parameter for the second interface. How to deal with a situation like this?

This problem is solved by global variables, which are defined as global variables: enable global variables: globals () ['varName']

Class TestMethod (unittest.TestCase): def test01 (self): print ('this is the first case') globals () [' userid'] = '1001' def test02 (self): print (userid) print (' this is the second case') if _ _ name__ = ='_ main__': unittest.main ()

At this point, test02 can successfully print out the value of userid. When you change test01 to test03, check the print value:

An error will be reported at this time, indicating that 'userid' is not defined? Because test02 is executed first, the unitest is sorted in alphabetical order. If there is a dependency, it can be sorted sequentially, but the dependency execution is minimized.

2. Skip case (do not execute a case)

Sometimes encountered in the test, only want to execute some case, other case does not execute, how to implement? Container @ unittest.skip (case name) will be used at this point.

Def test01 (self): print ('this is the first case') globals () [' userid'] = '1001' @ unittest.skip (' test02')

3. Container

The unittest.main () method in the program executes the testcase in all the unitest, so is there any other way to execute it?

Create a container, a collection of case, and add the case you need to run to the container to execute:

If _ _ name__ = ='_ _ main__': # create a container named suite suite=unittest.TestSuite () # add case suite.addTest (TestMethod ('test01')) # add the container to it to execute unittest.TextTestRunner () .container (suite)

If only one case is added to the container, only one use case will be executed, not all of them.

The default execution order is the ASCII code order of TestCaseName. After calling the addTest () method, what is added first is executed first, then added and then executed, and those that are not added are not executed. But the addTest method didn't work here, and two use cases, test01 and test02, were executed.

The reason is that in pycharm, the unittest module is introduced and will be executed in unittest mode by default. You need to convert the unittest schema to normal mode.

The method of converting unittest schema to normal mode:

If the modification is successful, the addTest takes effect, and only the use case test01 is executed

Think about it: if our case is in n py files, which way should we add case from different py files?

This is the end of the introduction on "how to manage case in automated testing unitest". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!

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

Development

Wechat

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

12
Report