Skip to content

2.3. Evaluate

Goal

  • Create a new test data file named exam_results.json which contains two names as keys, the values should be dictionaries which contain exam results like: {'math': '90%', 'physics': '85%'}.
  • Create a new suite named 02-Exams in 02-Classroom suite.
  • Create a new test case named Get Exam Results in 02-Exams suite which will read the exam_results.json and log the results to the console in the following format: Jane Doe: {'math': '90%', 'physics': '85%'}.

Solution

Hints

JSON (JavaScript Object Notation) is a lightweight data-interchange format.

Click here to learn more about the JSON format. Click here to see examples for JSON.

You can decode JSON strings by using Python module named json.

Click here to learn more about json module.

You can use Evaluate keyword to evaluate expressions in Python.

Click here to learn more about Evaluate keyword.

Robot Framework supports native &{dict} iteration with FOR loops from version 3.2.0`. Make sure you have at least that version of Robot Framework installed!

Solution: tests/02-classroom/test_data/exam_results.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "Jane Doe": {
        "math": "90%",
        "physics": "85%"
    },
    "John Doe": {
        "math": "85%",
        "physics": "90%"
    }
}
Solution: tests/02-classroom/02-exams.robot
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
*** Test Cases ***
Get Exam Results
    ${json_content}=    Get File    ${EXAM_RESULTS_JSON}
    &{results}=    Evaluate    json.loads('''${json_content}''')    json
    Log To Console    ${\n}    no_newline=True
    FOR    ${student}    ${results}    IN    &{results}
        Log To Console    ${student}: ${results}
    END

*** Settings ***
Library    OperatingSystem

*** Variables ***
${EXAM_RESULTS_JSON}    ${CURDIR}${/}test_data${/}exam_results.json

Results

Inside the tests folder, execute the following command to execute 02-Exams suite.

1
robot --suite 02-Exams .

You can check the generated log.html file to see how your test cases worked.


Last update: June 22, 2020