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 realize the Python Test Module

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

Share

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

Editor to share with you how to implement the Python testing module, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

To make it easier to implement this Python test module, avoid copying and pasting test functions multiple times, and setting unique names, both py.test and nose support derived tests, which are actually an iterator, and then use its yield statements and provide call parameters.

Later, however, they all encounter the same situation: they must examine the list of modules to find the functions and classes that developers want to run as tests. As you saw in the previous article, py.test tends to choose a single standard and expects all projects that use it to follow it; nose allows richer customization, but at the expense of behavioral predictability.

The same is true for test findings: py.test detects tests in the test module according to fixed, immutable, and predictable rules, while nose uses flexible and customizable rules. If the project uses nose to perform tests, you must first read the project's Python test module to understand whether nose uses the usual test detection rules or the project-specific rules.

The following is the process used by py.test:

Class Category (models.Model): id = models.AutoField ('id', primary_key=True) name = models.CharField (maxlength=50) code = models.CharField (maxlength=50) parentCategory = models.ForeignKey (' self', 'id' Null=True) enable = models.BooleanField () def _ _ str__ (self): return self.name class Admin: list_display = ('id',' name', 'code',' parentCategory')

When py.test examines the interior of the Python test module, it collects every function whose name begins with test_ and every class whose name begins with Test. It collects classes regardless of whether they inherit from unittest.TestCase or not. The test function runs directly, but for the test class, you must also search for methods. After the class is instantiated, run all methods whose names begin with test_ as a test.

If the test class inherits from the standard Python unittest.TestCase class, the py.test framework behaves oddly: if the class does not contain the runTest () method, py.test will throw an exception and fail even if it contains several test_ methods. However, if there is a runTest () method, py.test ignores it; this method must exist for py.test to accept the class, but it will not run because its name does not start with test_.

To correct this behavior, you can activate the framework's unittest plug-in in the project's conttest.py file or with the-p command line option: this causes py.test to make three changes to its behavior. First, it no longer detects only classes whose names begin with Test, but also other classes that inherit from unittest.TestCase.

Second, for TestCase subclasses that do not provide a runTest () method, py.test no longer reports exceptions. Third, run all the setUp () and tearDown () methods in the TestCase subclass correctly in a standard manner before and after the tests contained in the class.

Although nose provides stronger customization capabilities, the relatively simple test discovery process is as follows: when nose examines the interior of the Python test module, it uses the regular expression used when selecting the test module. Collect functions and classes that match this regular expression. By default, look for names that contain the words Test or test, but you can provide different regular expressions through the command line or configuration file.

When nose examines the interior of the test class, it runs methods that match the same regular expression. There is no need to specify that nose always detects subclasses of unittest.TestCase and uses them as tests. However, it determines which methods are tests based on its own regular expressions, instead of using the standard unittest pattern ^ test.

The above is all the contents of the article "how to implement the Python Test Module". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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