This is part of the REST API Testing in Python series.
In part 1 of the series, I’ve shown it is easy to write REST API tests in Python. However, if you are writing a test framework for a team where some people are not good at coding, or just to make life easier for everyone, the approach I am going to introduce in this post may be a solution for you.
TLDR: It reads plain text input files to send REST API requests, converts the JSON response to INI file format and saves it as output files, then compares the output files with expected files having exceptions defined in ignore files. To create new cases, simply create more input files; to create expected files, simply examine the actual output files and copy them as expected files if passed.
Note: This approach could be implemented in any language, though I will show it in Python.
Project Structure
The project structure is as follows.
tree
├── inputs
│ ├── test_case_01
│ │ ├── request_01.ignore
│ │ ├── request_01.txt
│ │ ├── request_02.ignore
│ │ └── request_02.txt
│ └── test_case_02
│ └── request_01.txt
├── outputs
│ ├── test_case_01
│ │ ├── response_01.txt
│ │ └── response_02.txt
│ └── test_case_02
│ └── response_01.txt
├── expects
├── diff
└── Scripts
└── test_rest_api.py
- inputs
API request content and ignore fields for comparison. - outputs
Received response converted to INI format - expects
Same as passed outputs - diff
output difference compared to expected files - Scripts
The main python test script
Parse Input Files
A sample request input file is as follows.
There are 3 parts separated by an empty line:
- Part 1: Request method + URL
- Part 2: Headers