mt523
mt523

Reputation: 88

How to pass object-variables to test cases in RobotFramework?

Here's what's going on (generically) with my python-based test.

==============================================================================
***Settings***

Variables       variable_file.py
Library         library.py

***Test Cases***

A Test Case
      myTest      ${some string}       [I want to pass an object here]

=============================================================================

At the end, I want to pass an actual class object which has been defined inside my variable file. I've looked far and wide without finding a way to pass anything beyond a string or a list. Does anyone know how?

Upvotes: 1

Views: 10637

Answers (2)

kontulai
kontulai

Reputation: 876

You can instantiate object variables in variable files as stated in User guide Here is a generic example

Variable file:

#var_file.py:
class foo(object):
    a = "foo"
    b = "bar"

MY_VAR = foo()
MY_SECOND_VAR = foo

Test case file:

#var_file.txt
*** Test cases ***
log variables
    log    ${MY VAR.a}
    log    ${MY VAR.b}

Execute using:

pybot -V var_file.py var_file.txt

Upvotes: 1

Laurent Bristiel
Laurent Bristiel

Reputation: 6935

By default, ${variable} are strings, but it can also contain objects. And you can pass it to keywords as argument.

Take a simple example like that:

${mydict} =  Create Dictionary  a  1  b  2

=> Then you have ${mydict} = {'a': '1', 'b': '2'}

And then you can call a keyword with your object as argument. For example:

Dictionary Should Contain Key  ${mydict}  a

I am using that kind of thing to test a REST API that sends me JSON. I store JSON objects in Robot variable and check the results by exploring the content of the dict. I explained it a little bit in a blog post.

Upvotes: 2

Related Questions