问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/17178984/how-to-pass-object-variables-to-test-cases-in-robotframework