JSON
| Введение | |
| Пример | |
| Похожие статьи |
Введение
Пример
Нужно прочитать из робота файл users.json и разобрать его содержимое
Структура проекта
json/ ├── data │ └── users.json └── src └── json_demo.robot 2 directories, 2 files
{ "users": [{ "username": "tester0", "password": "secret0", "firstname": "Dmitry", "lastname": "Mendeleev", "phone": 12345 }, { "username": "tester1", "password": "secret0", "firstname": "Nikolai", "lastname": "Basov", "phone": 12346 } ] }
Будем использовать две Robot Framework библиотеки: Collections и OperatingSystem
# json_demo.robot *** Settings *** Library OperatingSystem Library Collections *** Test Cases *** Loading JSON ${json_f}= Get File ../data/users.json ${object}= Evaluate json.loads('''${json_f}''') json Log To Console ${\n}${object["users"][0]["username"]} Creating Sub Dicts ${json_f}= Get File ../data/users.json ${object}= Evaluate json.loads('''${json_f}''') json &{user0}= Convert To Dictionary ${object["users"][0]} Log To Console ${\n}${user0} Log To Console ${\n}${user0["lastname"]}
python -m robot json_demo.robot
============================================================================== Json Demo ============================================================================== Loading JSON .. tester0 Loading JSON | PASS | ------------------------------------------------------------------------------ Creating Sub Dicts ... {'username': 'tester0', 'password': 'secret0', 'firstname': 'Dmitry', 'lastname': 'Mendeleev', 'phone': 12345} . Mendeleev Creating Sub Dicts | PASS | ------------------------------------------------------------------------------ Json Demo | PASS | 2 tests, 2 passed, 0 failed ============================================================================== Output: C:\json\src\output.xml Log: C:\json\src\log.html Report: C:\json\src\report.html
Напишем несколько тестов, проверяющих наличие файла и его содержимое.
json/ ├── data │ └── users.json └── src ├── json_demo.robot └── json_test.robot 2 directories, 3 files
# json_test.robot *** Settings *** Library OperatingSystem Library Collections *** Variables *** ${json_file}= ../data/users.json *** Test Cases *** Loading JSON ${exists}= File Should Exist ${json_file} ... File `${json_file}` doesn't exist ${json_f}= Get File ${json_file} Should Not Be Empty ${json_f} ... JSON file `${json_file}` should not be empty ${my_json}= Evaluate json.loads('''${json_f}''') json Should Be True isinstance(${my_json}, (dict)) Set Suite Variable ${my_json} Username 0 Is Correct ${username0}= Get Variable Value ${my_json["users"][0]["username"]} Log To Console ${\n}${username0} Should Be Equal As Strings tester0 ${username0} ... Expected `tester0` but got `${username0}` User 0 Dict &{user0}= Convert To Dictionary ${my_json["users"][0]} Log To Console ${\n}${user0} Should Be Equal As Strings {'username': 'tester0', 'password': 'secret0', 'firstname': 'Dmitry', 'lastname': 'Mendeleev', 'phone': 12345} ... ${user0} Lastname 0 Is Correct &{user0}= Convert To Dictionary ${my_json["users"][0]} ${user0_last}= Get Variable Value ${user0["lastname"]} Log To Console ${\n}${user0_last} Should Be Equal As Strings Mendeleev ${user0_last} ... Expected `Mendeleev` but got `${user0_last}`
============================================================================== Json Test ============================================================================== Loading JSON | PASS | ------------------------------------------------------------------------------ Username 0 Is Correct . tester0 Username 0 Is Correct | PASS | ------------------------------------------------------------------------------ User 0 Dict . {'username': 'tester0', 'password': 'secret0', 'firstname': 'Dmitry', 'lastname': 'Mendeleev', 'phone': 12345} User 0 Dict | PASS | ------------------------------------------------------------------------------ Lastname 0 Is Correct .. Mendeleev Lastname 0 Is Correct | PASS | ------------------------------------------------------------------------------ Json Test | PASS | 4 tests, 4 passed, 0 failed ==============================================================================
Указать путь от текущей директории можно с помощью Normalize Path и CURDIR
${path}= Normalize path ${CURDIR}/../../../../data/users.json ${json}= Get File ${path}
Автор статьи: Андрей Олегович