Python + Selenium + unittest Adorner @classmethod

Source: Internet
Author: User
Tags static class

Objective

In the previous unittest, the setup can be executed before each execution of the use case, which effectively reduces the amount of code, but there is a drawback, such as opening a browser operation, which is reopened every time the use case is executed, which wastes a lot of time.

So it is possible to open a browser only once, execute the use case and then close it? This requires the use of adorners (@classmethod) to solve.

First, the decoration device

1. Using Setup to differentiate from SetupClass

Setup (): Run before each test case runs
Teardown (): Executes after each test case finishes running
SetupClass (): Must use @classmethod adorner, all case runs only once before running
Teardownclass (): Must use @classmethod adorner, all case runs once after run

[Email protected] is a modifier, Classmethod is a class method in Python

Ii. Order of execution

1. Use the class method to write a few simple case,

#Coding:utf-8ImportUnitTestImport TimeclassTest (unittest. TestCase): @classmethoddefSetupClass (CLS):Print "start!"@classmethoddefTeardownclass (CLS): Time.sleep (1)        Print "end!"    deftest01 (self):Print "Execute test Case"    deftest03 (self):Print "Execute test Case"    deftest02 (self):Print "Execute test Case"    defaddtest (self):Print "Add Method"if __name__=="__main__": Unittest.main ()

2. From the execution results it can be seen that the predecessor and the post are executed only once prior to the execution of the use case.

start!
Execute test Case 01
Execute test Case 02
Execute test Case 03
... end!

----------------------------------------------------------------------
Ran 3 Tests in 1.001s

3. Noun interpretation

These three words appear in the Python class, and the self and the CLS can be replaced by other words, and there are three methods of the class.

One is defined by Def, the ordinary general, need to pass at least one parameter, generally with self, such a method must be accessed through an instance of a class, similar to C + + through the object to access;

The second is to add @classmethod in front of Def, this kind of method is a feature that can be called by the class name, but also must pass a parameter, generally with the CLS to represent class, that can be called directly through the class;

The third is to add @staticmethod in front of Def, this class method is static class method, similar to C + + static function, one of his characteristics is that the parameter can be empty, also support the class name and object two kinds of call mode;

Python + Selenium + unittest Adorner @classmethod

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.