Robot Framework test library of self-defined scope of understanding

  robot framework, a powerful test library api support, user-defined test according to the actual needs of the library, after you import custom libraries can be used in the keywords.

  When the custom test library is a class library, you need to consider a question: class instance. With class libraries can achieve internal states, which can be keyword or modify the constructor. Because these states will affect the actual behavior of the keyword, therefore, to ensure that a test case will not accidentally affect another use case is very important. this dependence behavior may cause very difficult to locate the bug. For example, adding a new test case, with the use of these libraries are not consistent manner.

  Robot Framework in order to ensure independence between test cases, by default, it creates a new test database instance for each test case. However, this is not always the way we want, for example, sometimes you need to share a test case when the state. in addition, those libraries stateless obviously do not need to have to create a new instance every time. Examples of the test library class attribute ROBOT_LIBRARY_SCOPE manner by three scopes defined to control

  1.TEST CASE: create test cases for each database instance, independently of one another by inter embodiment, it is by default;

  Create a test library classes: GTest.py

 1 class GTest(object):
 2 
 3     ROBOT_LIBRARY_SCOPE = "TEST CASE"
 4     counter = 0
 5 
 6     def __init__(self):
 7         GTest.counter += 1
 8 
 9     def count(self):
10         return self.counter, id(self)
View Code

   Create a test suite login.robot:

 1 *** Settings ***
 2 Documentation     Suite description
 3 Library           GTest.py
 4 
 5 *** Test Cases ***
 6 Test title
 7     [Tags]    DEBUG
 8     @{res}   count
 9     log many    @{res}
10 
11 Test title2
12     [Tags]      DEBUG
13     @{res}   count
14     log many    @{res}
View Code

 From the view of the results of the use case, each of the test cases using GTest runtime library, counter value by 1, and the instance id different, so as SCOPE "TEST CASE" test libraries creates an instance for each test case, this configuration is more suitable for stringent control scenario by the interaction between the embodiment;

  2.TEST SUITE: Create a test database instance for each test suite, all use cases share the database instance in the test suite

  Modify the test class library GTest.py, ROBOT_LIBRARY_SCOPE will replace the value "TEST SUITE"

 1 class GTest(object):
 2 
 3     ROBOT_LIBRARY_SCOPE = "TEST SUITE"
 4     counter = 0
 5 
 6     def __init__(self):
 7         GTest.counter += 1
 8 
 9     def count(self):
10         return self.counter, id(self)
View Code

  New Test Suite login2.robot

 1 *** Settings ***
 2 Documentation     Suite description
 3 Library           GTest.py
 4 
 5 *** Test Cases ***
 6 Test title
 7     [Tags]    DEBUG
 8     @{res}   count
 9     log many    @{res}
10 
11 Test title2
12     [Tags]      DEBUG
13     @{res}   count
14     log many    @{res}
View Code

  Run the test suite login.robot and login2.robot

 

 

 

 

 From the above test results, in Figure 1, all the test cases in the same test suite to obtain the same GTest example; in FIG. 2, the different test suite different acquired GTest example, and thus when "TEST SUITE" SCOPE setting, create a test database instance for each test suite;

  3.GLOBAL: Throughout the testing process only create a test database instance, all test suites, test cases share the same instance of a test library

  Modify the test library GTest.py, will ROBOT_LIBRARY_SCOPE changed to "GLOBAL"

 1 class GTest(object):
 2 
 3     ROBOT_LIBRARY_SCOPE = "GLOBAL"
 4     counter = 0
 5 
 6     def __init__(self):
 7         GTest.counter += 1
 8 
 9     def count(self):
10         return self.counter, id(self)
View Code

  Run the test suite login.robot and login2.robot

 

 

 From the results of Examples run by run, and the test suite login.robot login2.robot the same instance GTest used, and only one. SCOPE Thus the set "GLOBAL", throughout the test example only generate a test library. All kits, test cases, test library shared this instance;

  robot's official website explained, if multiple imports in order to test different parameters in the same library, regardless of whether ROBOT_LIBRARY_SCOPE defined, each test suite creates a new instance

  Create a test library GTest.py, ROBOT_LIBRARY_SCOPE defined as the "GLOBAL"

 1 class GTest(object):
 2 
 3     ROBOT_LIBRARY_SCOPE = "GLOBAL"
 4     counter = 0
 5 
 6     def __init__(self, *args):
 7         self.args = args
 8         GTest.counter += 1
 9 
10     def count(self):
11         return self.counter, id(self)
View Code

  Create a test suite login.robot

 1 *** Settings ***
 2 Documentation     Suite description
 3 Library           GTest.py      suite1      login1
 4 
 5 *** Test Cases ***
 6 Test title
 7     [Tags]    DEBUG
 8     @{res}   count
 9     log many    @{res}
10 
11 Test title2
12     [Tags]      DEBUG
13     @{res}   count
14     log many    @{res}
View Code

  Create a test suite login2.robot

 1 *** Settings ***
 2 Documentation     Suite description
 3 Library           GTest.py      login2      suite2
 4 
 5 *** Test Cases ***
 6 Test title
 7     [Tags]    DEBUG
 8     @{res}   count
 9     log many    @{res}
10 
11 Test title2
12     [Tags]      DEBUG
13     @{res}   count
14     log many    @{res}
View Code

 

From the results of test run of view, it generated different examples, while the scope of the definition has become global, but due to the different parameters introduced in different test suite, the use during the execution of a different embodiment of the test suite.

Guess you like

Origin www.cnblogs.com/blackeyes1023/p/11613463.html