Powerful Python testing framework

Source: Internet
Author: User

All my descriptions of FP are implemented using the Python testing framework. But the most important feature-probably the most useful feature in a specific situation-is that it eliminates side effects or at least has some restraining effect on some special fields, such as a single body ).

The vast majority of program errors-and issues that lead programmers to turn to debugging for solutions-occur because variables contain unexpected values during program execution. Function programs do not assign values to variables at all, thus avoiding this special problem.

Let's look at a fairly common command code. It is used to print a list of digits with a multiplier value greater than 25. The numbers that make up each pair are selected from the other two lists. This operation is similar to the Operations actually performed by programmers in their program segments. The command to achieve this is as follows:


This project is too small to cause any errors. However, our goal may be embedded in code that needs to implement many other purposes at the same time. Comments with "more stuff" are the places where side effects may cause errors. In any of these places, the variables xs, ys, bigmuls, x, and y may obtain unexpected values in the hypothetical Section code. Besides, after executing this code, all variables may have values that may or may not be needed later.

Obviously, you can use function/instance encapsulation and related scope considerations to prevent such errors. Besides, you can always del the variables after they are executed. However, in practice, these error types are very common.

For the Python testing framework, you will find some interesting phenomena in the Zope community. The Zope community does not build large tools to solve every problem, but rather small tools with limited functionality that can be connected together. Currently, the zope. testing module does not provide a detection and testing mechanism!

Instead, zope. testing allows programmers to find the tests to be run in each module and put them in a list. It only looks for one thing in each test module: test_suite () function. This function should return an instance of the standard unittest. TestSuite class, which contains a test defined by the module.

Some programmers who use zope. testing manually create and maintain the test list in the test_suite () function. Other programmers can write custom code to discover available tests that have been defined. However, the most interesting method is to use another Zope package z3c. testsetup, which can automatically discover tests in the package like other modern Python testing frameworks.

This phenomenon once again demonstrates that Zope programmers tend to write small code blocks and then use them to build frameworks, rather than writing large, comprehensive solutions. The z3c. testsetup package does not contain the command line interface that can be selected for testing, or the output module that can display test results. It relies entirely on zope. testing to implement these functions.

In fact, z3c. testsetup users generally do not use the zope. testing test module discovery function. Instead, they bypassed the zope. testing Algorithm and looked for only the module named test. py according to its default behavior. Then, only one module with this name is provided in the entire source code tree. In the simplest case, their test. py is like the following:

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.