Reputation: 65
I am fairly new to Robot Framework and I have written a few keywords, which invokes methods in a .py file. All these Python methods are placed in a class in the file llk_machine.py.
Below is my TestCase file:
*** Settings ***
Variables /repo/user/var_attero.py
Library %{ROBOTREPO}/Lib/LIB_LLK/llk_machine.py WITH NAME class1
*** Keywords ***
CONNECT
[Documentation] Connects to Machine.
${CONNECT_PASS_or_FAIL}= class1.llk_Connect
[Return] ${CONNECT_PASS_or_FAIL}
CONFIGURE_DELAY
[Arguments] ${port_number}=1 ${fixed_delay}=25000
[Documentation] Configure impairment in the active testbed.
${pass_fail_status}= class1.llk_Config_Impair_fixed ${port_number} ${fixed_delay}
[Return] ${pass_fail_status}
llk_Connect and llk_Config_Impair_fixed are two methods in Python file. Through keyword connect I am connecting to the machine. When I execute this testcase it executes just fine and passes. Then when I select the second test case for configuring the delay, I get an error saying "Not able to connect the machine". But I have already connected to the machine through the first testcase and when I checked physically, the machine is connected to my computer.
So I think that another object is being instantiated when I select the second keyword and a new socket connection to the machine is being requested which is obviously failing as the machine is already talking to other socket and can't accept another connection.
When I execute both the test cases simultaneously, it is working just fine. The problem arises when I execute them separately, one after another.
How to prevent the creation of new object for every test case and have only one object throughout the entire test cycle.Please help me out.
Thanks.
Upvotes: 3
Views: 2834
Reputation: 386010
By default, robot will create a new instance of a keyword library for each test case, specifically so that test cases don't inherit state from other test cases.
You can change this behavior by adding a special variable in the python file or class that implements the keywords. This variable is named ROBOT_LIBRARY_SCOPE
. You can set it to one of the following literal strings:
"TEST CASE"
- A new instance is created for every test case. A possible suite setup and suite teardown share yet another instance. This is the default.
"TEST SUITE"
- A new instance is created for every test suite. The lowest-level test suites, created from test case files and containing test cases, have instances of their own, and higher-level suites all get their own instances for their possible setups and teardowns.
"GLOBAL"
- Only one instance is created during the whole test execution and it is shared by all test cases and test suites. Libraries created from modules are always global.
Note: the above descriptions are from the section Test library scope in the robot framework user guide.
For example, to have the state shared between all tests in a suite, the top of your python file might would look something like this:
# llk_machine.py
ROBOT_LIBRARY_SCOPE = "TEST SUITE"
...
def llk_Connect(...):
...
If your keywords are methods on a class with the same name of the file, you should set the variable as an attribute of the class
# llk_machine.py
class llk_machine(...):
ROBOT_LIBRARY_SCOPE = "TEST SUITE"
...
More complete examples can be found in the "Test library scope" section of the robot framework user guide
Upvotes: 5