Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Data Driven REST API Test Examples with Robot Framework
#! Robot Framework
*** Settings ***
Metadata Version 0.1.0
Metadata Author *Tset Noitamotua*
Metadata Created 2019.01.23
Documentation Demo - Data Driven Test with RF
...
... Template set within Test Case
...
... PRO: executing with `robot --randomize` will preserve the order within
... the test case
...
... CONTRA: may be bad for statistics
...
Library String
Suite Setup Server is ready and the user is logged in
Test Template client sends POST request to /endpoint
Force Tags public
*** Test Cases ***
Example of [Template] settings inside test case
[Template] client sends POST request to /endpoint
# SUBJECT_ID SUBJECT_NAMESPACE EXPECTED RESPONSE CODE
${EMPTY} ${EMPTY} 400
${EMPTY} snamespace_1234 400
valid id ${EMPTY} 400
valid id snamespace_1234 200
valid id snamespace_1234 200
12345679 snamespace_1235 200
invalid id snmspace_1234 500
valid id %%% 500
12345679 snamespace_1235 208
12345680 snamespace_xxx 200
00000001 asdf 200
%%%%%%% %%% 400
% -------- 400
%%% /// 400
valid id % 500
valid id %% 500
valid id %%% 400
valid id %%% 400
valid id %^$£&@#~ 400
%^$£&@#~ %^$£&@#~ 400
*** Keywords ***
# TEST TEMPLATE KEYWORD (the actual test flow)
client sends POST request to /endpoint
[Arguments] ${arg1} ${arg2} ${arg3}
# STEP 1
${subid}= process arguments ${arg1}
Set Suite Variable ${SUBJECTID} ${subid}
# STEP 2
# simulating a request (not actually sending one), but here is how it coould look like
# &{r}= POST /endpoint?subjectId=${subid}&subjectNamespace=${arg2}
Log To Console \nPOST /endpoint?subjectId=${subid}&subjectNamespace=${arg2}
# STEP 3
# simulating a response and validating against expected result
${response}= Evaluate random.choice([200, 200, 200, 208, 500, 400]) random
#Log To Console \nresponse: ${response}
#Log To Console expected: ${arg3}
${response} status code should be ${arg3}
process arguments
[Arguments] ${subject_id}
${valid id} = Run Keyword And Return If "${subject_id}"=="valid id" generate valid subject_id
${invalid id} = Run Keyword And Return If "${subject_id}"=="invalid id" generate invalid subject_id
[Return] ${subject_id}
generate valid subject_id
${s}= Generate Random String 3 [LOWER]
${i}= Generate Random String 4 [NUMBERS]
${subject_id}= Set Variable ${s}${i}
[RETURN] ${subject_id}
generate invalid subject_id
${s}= Generate Random String 3 %^$£&@#~
${i}= Generate Random String 4 1234567890
${subject_id}= Set Variable ${s}${i}
[RETURN] ${subject_id}
server is ready and the user is logged in
Log Server is UP
Log Client logged IN
${response} status code should be ${code}
Should Be Equal As Strings ${response} ${code}
#! Robot Framework
# fully self contained and executable example.
# download the file and run with `robot data-driven-rest-api-test_Robot-Framework.robot`
*** Settings ***
Metadata Version 0.1.0
Metadata Author *Tset Noitamotua*
Metadata Created 2019.01.23
Documentation Demo - Data Driven Test with RF
...
... Template set within Settings Table
Library String
Suite Setup Server is ready and the user is logged in
Test Template client sends POST request to /endpoint
Force Tags public
*** Test Cases *** SUBJECT_ID SUBJECT_NAMESPACE EXPECTED RESPONSE CODE
# TEMPATED TEST CASE
Missing subjectId and subjectNamespace ${EMPTY} ${EMPTY} 400
Missing subjectId ${EMPTY} snamespace_1234 400
Missing subjectNamespace valid id ${EMPTY} 400
Valid subjectId and subjectNamespace 1 valid id snamespace_1234 200
Valid subjectId and subjectNamespace 2 valid id snamespace_1234 200
Valid subjectId and subjectNamespace 3 12345678 snamespace_1235 200
Same id but diff. namespace is no dupli. 12345678 snamespace_xxx 200
Invalid subjectId (symbols %^$£&@#~) invalid id snmspace_1234 500
Invalid subjectNamespace (%%%) valid id %^$£&@#~ 500
Invalid subjectId zero zero zero 00000000 asdf 200
*** Keywords ***
# TEST TEMPLATE KEYWORD (the actual test flow)
client sends POST request to /endpoint
[Arguments] ${arg1} ${arg2} ${arg3}
# STEP 1
${subid} = process arguments ${arg1}
Set Suite Variable ${SUBJECTID} ${subid}
# STEP 2
# simulating a request (not actually sending one), but here is how it coould look like
# &{r}= POST /endpoint?subjectId=${subid}&subjectNamespace=${arg2}
Log To Console \nPOST /endpoint?subjectId=${subid}&subjectNamespace=${arg2}
# STEP 3
# simulating a response and validating against expected result
${response}= Evaluate random.choice([200, 200, 200, 200, 500, 400]) random
#Log To Console \nresponse: ${response}
#Log To Console expected: ${arg3}
${response} status code should be ${arg3}
process arguments
[Arguments] ${subject_id}
${valid id} = Run Keyword And Return If "${subject_id}"=="valid id" generate valid subject_id
${invalid id} = Run Keyword And Return If "${subject_id}"=="invalid id" generate invalid subject_id
[Return] ${subject_id}
generate valid subject_id
${s}= Generate Random String 3 [LOWER]
${i}= Generate Random String 4 [NUMBERS]
${subject_id}= Set Variable ${s}${i}
[RETURN] ${subject_id}
generate invalid subject_id
${s}= Generate Random String 3 %^$£&@#~
${i}= Generate Random String 4 1234567890
${subject_id}= Set Variable ${s}${i}
[RETURN] ${subject_id}
server is ready and the user is logged in
Log Server is UP
Log Client logged IN
${response} status code should be ${code}
Should Be Equal As Strings ${response} ${code}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.