Reputation: 55
I have a test where I need to write the results into a database. I want to set up the connection to database (using username, password, database, host) via startSuite function in listener (which will run at the beginning of all testcases) and close it in endSuite. My question is, how do I pass the connection (or cursors) back to Robot Framework code to use in testcases. Currently I am doing this:
*** Test Cases ***
RecordinTestflow
Setup1
${return} = Record Start in Testflow ${data}
where Setup1 is a python function which will setup the connection and RecordStartinTestFlow will use that connection. I want to move the Setup1 to a listener python script.
Thank you.
Upvotes: 3
Views: 6565
Reputation: 386382
An external listener can't send information to a test case. However, if you use a keyword library as a listener, it can. The downside is that you have to import the listener in the test suite, rather than specify it on the command line.
The robot framework user guide has a section title Test libraries as listeners which describes how to do it.
Here's a contrived example showing how a listener method can set a suite variable which the test case can then use.
First, the listener:
from robot.libraries.BuiltIn import BuiltIn
class ListenerExample(object):
ROBOT_LISTENER_API_VERSION = 2
ROBOT_LIBRARY_SCOPE = "GLOBAL"
def __init__(self):
self.ROBOT_LIBRARY_LISTENER = self
def _start_suite(self, name, attrs):
message = "hello, world"
BuiltIn().set_suite_variable("${from listener}", message)
Next, a simple test case that shows how the variable gets set as soon as the suite starts. Notice that the test itself doesn't define ${from listener}
. Instead, it gets defined as soon as the listener method is called.
*** Settings ***
| Library | ListenerExample.py
*** Test Cases ***
| Example of getting data from a listener
| | should be equal | ${from listener} | hello, world
In your case, of course, you would change message
to be your database cursor or whatever else you want it to be.
Of course, you can also put keywords in this library that you can use as well.
Upvotes: 4