In-depth analysis of Python object-oriented language content

Source: Internet
Author: User

Python object-oriented language must perform some complex operations, such as opening a webpage in Firefox and clicking the "Continue" button, and then check the results. Before you start a test, such as opening the page and clicking the button, you must complete the test.

Now, consider what if the test is performed in all the one hundred functional tests. They all need to run Firefox by calling the common setup routine. Then you can perform your own test. The teardown Code may also be used to cancel setup. In this way, two hundred additional function calls will be added to the test suite. Each test function is as follows:

 
 
  1. # Naming a with_setup decorator  
  2.  
  3. firefox_test = with_setup(firefox_setup, firefox_teardown)  
  4.  
  5. @firefox_test  
  6. def test_index_click():  
  7.     ...  
  8.  
  9. @firefox_test  
  10. def test_index_menu():  
  11. ... 

To eliminate this duplicate code, many test frameworks provide a one-time setup and teardown code mechanism to specify the required code for each test. The three frameworks zope. testing, py. test, and nose discussed in this article support standard setUp () and tearDown () routines in the unittest. TestCase class compiled by programmers.

However, in addition, the features provided by the framework of the Python object-oriented language for the common setup code are significantly different. Zope. testing does not provide additional support for setup and teardown. However, the z3c. testsetup extension discussed earlier will make some interesting operations on doctest.

It searches for the Test-Layer: string in the file to find the Test. The layer in doctest can actually specify one of the two values. If doctest is labeled as a unit layer, it means that no special setup is required to run it. However, if you label it as a functional layer, it means you can only run it after calling the framework setup function.

Generally, the Test-Layer: functional Test is designed to run when the Zope Web framework is fully configured. Therefore, they can create test browser instances, send requests, and view the responses returned by the Web framework. By executing setup on behalf of doctest, z3c. testsetup can avoid copying a large amount of sample code in each functional doctest.

The last convenient feature to reduce Sample Code is to provide z3c. testsetup with a list of variables pre-loaded into the namespace of each doctest unit. And a list of variables pre-loaded to each functional doctest. In this way, you do not need to copy the same set of import statements at the beginning of each doctest file.

By default, Python does not support setup and teardown. It does not even run the setUp () and tearDown () Methods of the standard unittest. TestCase class unless it opens its unittest plug-in. Nose is the best in supporting joint test code. When looking for a test, nose trace records find the test context. It considers that each test method in the unittest. TestCase subclass is "internal" of this class ".

Therefore, it is controlled by its setUp () and tearDown () methods. It also considers that the test exists in their modules, the package containing the modules, and the "internal" of all the outer packages ". Therefore, for tests in multi-layer "concentric" containers, nose runs the setup code in all containers before running the test, and runs the teardown code in all containers after running the test.

Finally, specify or as unittest in the @ with_setup modifier. the setup and teardown functions provided by the methods in the TestCase subclass run once for each function or test, the setup and teardown codes provided to nose at the module level or package level only run once for the entire test set. Therefore, do not consider such tests completely isolated from each other: they will share the copies of resources created in the setup routine of the module or package.

  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?

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.