I'd have a library with a single function to safely get a key from a dictionary. Is this possible inside a test suite?
def safe_get(dict_obj, key):
val = dict_obj.get(key)
if val is None:
val = {}
return val
Is there a way to do this using something like Run_Keyword_If
or Set_Variable_If
?
To pass a dictionary to a keyword, you do it like any other argument. In your case, if you have a dictionary named ${Participants} , you would pass it as ${Participants} . As for iterating over the dictionary, you need to replace $ with @ , and use FOR/IN.
Enter the argument to be used with the keyword. Go back to your test case. Now, you need to pass the value which is the URL to be used for the test case. In the test case, when you type the user-defined keyword and press Ctrl + Spacebar, it gives the details of the keyword along with the arguments.
Using the get() Method. The get() method delivers the value of the related key in a dictionary. If the key isn't there, it either returns None or a default value (if one was supplied). We can pass a key to this method and see if it exists in the given Python dictionary.
Assuming you're using robot 2.9 or later, you can call the get
method on the dictionary by using the Evaluate keyword, which will allow you to specify a default value when the key doesn't exist.
For example:
| | ${data}= | create dictionary | ...
| | ${value}= | evaluate | $data.get("some key", "default value")
Starting with robotframework 2.9 you can directly access variables in expressions by removing the curly braces (see Evaluating Expressions in the BuiltIn library documentation). For example, if you have a dictionary named ${data}
, you can use the actual variable in an expression with $data
.
This makes it very easy to use variables in python expressions. For example, if you want to provide a default for when a dictionary doesn't have a key you can call the get
method of the dictionary by using the Evaluate keyword with something like $data.get(...)
.
Note also that robot defines the variable ${None}
to be the python value None
(not the string "None"
), which you can use in an expression for checking to see whether a value is None
.
The following examples show how to use evaluate
to call the get
method of the dictionary. The first test shows that you can check for a None
value, the second example shows how you can provide a default value.
*** Settings ***
| Library | Collections
*** Test Cases ***
| Get value from dictionary, returning None if key not in dictionary
| | ${data}= | Create dictionary | key1=one | key2=two
| | ${value}= | Evaluate | $data.get("key3")
| | should be equal | ${value} | ${None}
| Get value from dictionary, returning default value if key not in dictionary
| | ${data}= | Create dictionary | key1=one | key2=two
| | ${value}= | Evaluate | $data.get("key3", "default value")
| | should be equal as strings | ${value} | default value
Of course, if you want your default value to be a new dictionary (as implied by your question) you can do that too:
| | ${value}= | Evaluate | $data.get("key3", {})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With