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 use unittest

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

Share

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

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

1. Unit testing

Unit testing refers to checking and verifying the smallest testable unit in the software. Python has a unit testing framework unittest module, which can be used not only for unit testing, but also for the development and execution of WEB automated test cases. the test framework can organize the execution of test cases, and provides rich assertion methods to judge whether the test cases pass or not, and finally generate test results.

Basic components in the 2.unittest library

Unittest.TestCase (): is the basic class inherited by all test case classes. As long as a class inherits the TestCase class in unittest as a test case class, it can write test cases in the class.

From unittest import TestCase Mainclass MyTest (TestCase): # inherit unittest.TestCase def setUp (self): # initialization before test case execution print ('start execution') def tearDown (self): # follow-up work after test case execution print ('execution complete') def testWay (self): print ('this is a test case') if _ _ name _ _ ='_ _ main__': main () # run all test cases

Unittest.TestSuite (): used to create a test suite with an addTest () method that adds one or more test cases to the test suite, and runs the method that is added first.

Unittest.TextTextRunner (): run the test case assembled by suite through the run () method in this class

Unittest.defaultTestLoader (): through the discover () method below this class, you can automatically find test case modules (files that start with test and end with .py, for example: test*.py) based on the start_dir matching of the test directory, and assemble the found test cases into the test suite, so you can execute discover directly through the run () method.

Unittest.skip (): decorator that filters out use cases that do not need to be executed so that this use case does not execute

Unittest.main (): you can easily turn a unit test module into a test script that can be run directly. The main () method causes the TestLoader class to search for all test methods contained in the module that start with the name "test" and execute them automatically.

Methods in the 3.TestCase class

The setUp () method is used for initialization before the test case is executed. If you need to access the database in the test case, you can establish a database connection in setUp and initialize it. If the test case needs to log in to web, you can get the cookie first and get the file handle

TearDown () method: used to clean up the aftermath of test case execution, such as closing database connections and closing files

Assert* (): assertion method, which is equivalent to conditional judgment in if-else. In the process of executing a test case, whether the final use case is passed or not is determined by determining whether the actual result of the test is equal to the expected result.

Assertion methods commonly used in 4.unittest

AssertEqual (self, first, second, msg=None): judge whether two parameters are equal, and if so, the test case passes

AssertNotEqual (self, first, second, msg=None): judge whether the two parameters are equal, and if not, the test case passes

AssertTrue (self, expr, msg=None): determine whether it is true or not, then the test case passes

AssertFalse (self, expr, msg=None): determine whether it is false, and if it is false, the test case passes

AssertIs (a, b, msg=None): asserts whether an is b, and if so, the test case passes.

More methods of assertion can be found on the official website: https://docs.python.org/3/library/unittest.html#assert-methods

5. Write test cases

First, import the classes needed by TestCase, main, etc., from the unittest module.

Then customize a class that inherits the TestCase class

You can override the setUp () initialization method and the tearDown () cleanup method in the TestCase class, which appear in pairs.

Then write test cases that begin with letters. By default, the order of execution is to load the test cases according to the order of ASCII codes. The order of numbers and letters is 0-9.

Finally, call the unittest.main () method to turn a unit test module into a test script that can be run directly, and then execute the test case automatically.

From unittest import TestCase, mainclass MyTestCase (TestCase): # setUp and tearDown execute a method at the beginning of test each time Will call def setUp (self): print ('setup') def tearDown (self): print (' tearDown') def test_1 (self): print ('first test case') def test_2 (self): print ('second test case') if _ name__ = ='_ main__': main () about how unittest So much for sharing this article with " Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please 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

Development

Wechat

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

12
Report