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:
- # Naming a with_setup decorator
-
- firefox_test = with_setup(firefox_setup, firefox_teardown)
-
- @firefox_test
- def test_index_click():
- ...
-
- @firefox_test
- def test_index_menu():
- ...
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.
- Python source code compilation skills
- Simple and Easy-to-use Python tools
- Introduction to the Python application field
- Python Android Object-Oriented Programming-Python applications
- How to Use the Python module to parse the configuration file?