Now, each of us is faced with a REST API that either develops or consumes such a service. In addition, we are in the era of microservices, where we slice up business logic into small independent services independent of each service. Most of these services follow RESTful principles and communicate using the JSON format, which is the most widely used format due to its simplicity.
Pyhttptest Command line tool for HTTP testing using RESTful apis.
The tool automates testing in a simple three-step process
- The installation
pip install pyhttptestCopy the code
- Describe an HTTP request test case against an API service using JSON, the simplest and most widely used format in the file
- Sending an HTTP GET request Json file contains the following content
{
"name": "TEST: Get server status",
"verb": "GET",
"endpoint": "/get",
"host": "https://httpbin.org",
"headers": {
"Accept-Language": "en-US"
}
}
Copy the code
—
- The content of the JSON file for sending an HTTP POST request is as follows
{ "name": "TEST: Create an HTML bin", "verb": "POST", "endpoint": "post", "host": "https://httpbin.org", "payload": { "content": "Hello, world!" }}Copy the code
- Multiple test case definition examples
[
{
"name": "TEST: List all users",
"verb": "GET",
"endpoint": "api/v1/users",
"host": "http://localhost:8085/",
"headers": {
"Accept-Language": "en-US"
},
"query_string": {
"limit": 1
}
},
{
"name": "TEST: Add a new user",
"verb": "POST",
"endpoint": "api/v1/users",
"host": "http://localhost:8085/",
"payload": {
"username": "pyhttptest",
"email": "[email protected]"
}
},
{
"name": "TEST: Modify an existing user",
"verb": "PUT",
"endpoint": "api/v1/users/XeEsscGqweEttXsgY",
"host": "http://localhost:8085/",
"payload": {
"username": "pyhttptest"
}
},
{
"name": "TEST: Delete an existing user",
"verb": "DELETE",
"endpoint": "api/v1/users/XeEsscGqweEttXsgY",
"host": "http://localhost:8085/"
}
]Copy the code
- Run the command and get the report
pyhttptest execute data/filename.jsonCopy the code
- Screenshots of reports from test cases
Properties of the test case
- Name – Name of the test case
- Verb – HTTP method
- Endpoint – The resource to be invoked on the server
- Host-server Indicates the IP address of the host
- Headers – An HTTP header. All HTTP headers
- Query_string – Query string – Query string parameter in the URL after the question mark
- Payload data –
Tips
One question that might occur to you is how to add, structure, and organize test cases into my existing/new project. Each Python project, which has tests contained in its project directory, has a folder called tests/.
From this directory, by convention, great frameworks such as UnitTest and pyTest discover and execute test cases defined in Python scripts. In order not to mess up these tests and break the rules, I recommend creating one called live_tests/ in your project root directory.
In the new directory, you can place all json files that define API test cases. By doing so, your tests will be easily distinguishable.
If you are interested, follow the public account “chasays” – a gathering place for programmers