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 write python unit tests

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge of how to write the python unit test. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Unittest introduction

The unittest framework has now been officially "incorporated" by python. As a library supported by the python standard package, unittest only needs to be imported directly when it is used, and does not need to be installed using pip.

How to write unit test code

The unit test code can be simply divided into three steps, namely Given,When,Then. Given represents the initial state or precondition (simply understood as data input); When represents the occurrence of the behavior, that is, the test action; and Then is the assertion result.

So in most cases, we can divide the unit test code into three pieces:

The first piece of code is responsible for data processing before testing, such as input data, etc.

The second block of code is responsible for calling the relevant functional modules for testing.

The third block of code is responsible for comparing the test results with the results we expect to get the test results (pass or fail).

Code actual combat

Taking a simple calculation function as an example, the following is the method of this calculation module (file name is calculator.py):

Def add (a, b): return a+bdef substract (a, b): return a Murb

We designed some simple test code (for more information on the code interpretation, see the notes):

From calculator import * # Import target module

Import unittest # Import unittest package

Class TestCount (unittest.TestCase):

# the test code is written in a test class

Def test_add (self):

# what we are going to test is an add function that passes in two parameters

X = 5

Y = 6

# enter test data

Res = add (x, y) # pass the data to the function under test, and then store the result in res

Self.assertEqual (res, 11) # uses assertions to determine whether the value of res is equal to the expected value (here expectation is 11)

# if the value of res is equal to the expected value, the test passes, otherwise the test fails

Def test_substract (self):

# this is another test code, using abbreviations to put the first and second steps together

Res = substract (11,6)

Self.assertEqual (res, 5)

The above code is only used to introduce how to use unittest, and the test cases are designed casually. In fact, test case design needs a lot of consideration, and it is necessary to prove the functional soundness of the target code through the test case, so for a simple addition function, such a test case is a little simple. The editor will continue to share the detailed test case design in the future, follow the W3C technical headlines and learn how to write better test cases! Python is a language that is easy to understand. In fact, unit testing is common only in more formal and large-scale projects. The more common use of unittest is to integrate with selenium into a testing framework, with unittest providing better use case management and assertions, and functional testing by selenium. At this point, unittest is no longer a unit testing tool (use case management tool). These are all the contents of the article "how to write python Unit Tests". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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