About the Python testing framework

Source: Internet
Author: User

For every IT industry employee, whether IT is a developer, Project Manager, or tester, mastering the Python testing framework will make our programming work easier and easier, the following is a study.

This article discusses how the three most popular test frameworks identify and collect tests, and how they support writing a complete test layer, sharing common setup and teardown code. The first article in these three series discusses the revolutionary impact of standard testing frameworks such as zope. testing, py. test, and nose on the Python testing field.

These frameworks support simpler testing methods, removing the need for projects to write and maintain specialized code for running tests. The second article discusses how these automated solutions search for Python packages to identify modules that may contain tests. This article discusses the next step and describes how the framework discovers the items to be tested after finding the test module. We also discuss some details, such as the support of the three frameworks for the common setup and teardown code.

For the zope. testing Framework, you will find that the Zope community has some interesting phenomena. The Zope community does not build large tools to solve every problem. Instead, it is building 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. By default, only the module named test. py is found, and 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. class Category(models.Model):     
  2.     id = models.AutoField('id', primary_key=True)     
  3.     name = models.CharField(maxlength=50)     
  4.     code = models.CharField(maxlength=50)     
  5.     parentCategory = models.ForeignKey('self', 'id', null=True)     
  6.     enable = models.BooleanField()     
  7.          
  8.     def __str__(self):     
  9.         return self.name     
  10.          
  11.     class Admin:     
  12.         list_display = ('id', 'name', 'code', 'parentCategory')   

This relies on the more powerful discovery mechanism provided by the Python testing framework instead of running the test discovery task through zope. testing. You can provide several configuration options to the register_all_tests () function. For details, see the z3c. testsetup document. Here you only need to introduce its basic behavior. Different from other frameworks discussed in this article, z3c. testsetup does not care about the name of each Python module in the package by default, but focuses on its content.

It checks all modules and all. txt or. rst files in the package, and selects the file with the Test-Layer: specified in the text. It then combines all TestCase in the module and all doctest parts in the text file to form a test suite.

Note that the Zope test framework only supports UnitTest instances or doctest instances. As discussed in the first article in this series, more modern Python testing frameworks also support General Python functions for effective testing. This requires different test and detection algorithms. We will see them in the Framework discussed below.

  1. Python source code compilation skills
  2. Simple and Easy-to-use Python tools
  3. Introduction to the Python application field
  4. Python Android Object-Oriented Programming-Python applications
  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.