Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tset-Noitamotua/27c70c5cdab0a94538defe6d605743fa to your computer and use it in GitHub Desktop.
Save Tset-Noitamotua/27c70c5cdab0a94538defe6d605743fa to your computer and use it in GitHub Desktop.
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}
@prasoonmathur1654
Copy link

*** Settings ***
Documentation Suite description
Library RequestsLibrary
Library Collections
Library DataDriver ../TestData/Resturl.csv

Suite Setup create the Session
Suite Teardown Close Session
Test Template GPV

*** Variables ***
${base_url} http://10.212.2.107:8181
${timeout} 1s

*** Test Cases ***
GPV ${TestCase} ${url}

*** Keywords ***
create the Session
${user_pass}= create list admin admin
create session gpvsession ${base_url} auth= ${user_pass}
GPV
[Arguments] ${url}
${headers}= create dictionary Content-Type=application/json
${res}= get request gpvsession ${url} headers=${headers}
log to console ${res.status_code}
log to console ${res.content}

Close Session
delete all sessions

I'm getting this error Can you pls help me what is the problem in my code.
robot gpv.robot

Gpv :: Suite description

GPV | FAIL |
Parent suite setup failed:
TypeError: init() takes 3 positional arguments but 20 were given

Gpv :: Suite description | FAIL |
Suite setup failed:
TypeError: init() takes 3 positional arguments but 20 were given

1 critical test, 0 passed, 1 failed

D:\E-Drive\Automation\data_model\RobotFramework\TestCase>robot gpv.robot

Gpv :: Suite description

GPV | FAIL |
Parent suite setup failed:
TypeError: init() takes 3 positional arguments but 20 were given

Gpv :: Suite description | FAIL |
Suite setup failed:
TypeError: init() takes 3 positional arguments but 20 were given

1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed

Output: D:\E-Drive\Automation\data_model\RobotFramework\TestCase\output.xml
Log: D:\E-Drive\Automation\data_model\RobotFramework\TestCase\log.html
Report: D:\E-Drive\Automation\data_model\RobotFramework\TestCase\report.html

D:\E-Drive\Automation\data_model\RobotFramework\TestCase>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment