Testing REST APIs easily in Python with pyhttptest

6,232 reads

Nowadays every one of us is facing REST APIs by either developing or consuming such a service. Also, we’re in the trendy era of microservices, where we splitting our business logic into small separate services independent from each one. Mostly these services follow RESTful principles and using the JSON format for communication, which became the most widely used format, because of its simplicity.

reactions

pyhttptest - A command-line tool for HTTP tests over RESTful APIs

reactions

This tool automate testing in a simple three steps.

reactions

1. Install the package

reactions

pip install pyhttptest

2. Describe your HTTP Requests test cases against your API service in a simplest and widely used format JSON within a file.

reactions

Single test case definition examples

reactions

To send an HTTP GET Request

Create a JSON file, for example, data/test_server_status.json { "name" : "TEST: Get server status" , "verb" : "GET" , "endpoint" : "/get" , "host" : "https://httpbin.org" , "headers" : { "Accept-Language" : "en-US" } } reactions

To send an HTTP POST Request

Create a JSON file, for example, data/test_create_html_bin.json { "name" : "TEST: Create an HTML bin" , "verb" : "POST" , "endpoint" : "post" , "host" : "https://httpbin.org" , "payload" : { "content" : "Hello, world!" } } reactions

Multiple test cases definition example

reactions

Create a JSON file, for example, data/requests.json

[ { "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" : "admin@pyhttptest.com" } }, { "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/" } ]

3. Run command and gain report

reactions

pyhttptest execute data/test_server_status.json

The report from the single test case

reactions

pyhttptest execute data/requests.json

The report from the multiple test cases

reactions

The properties, which you can pass to a .

json

reactions

name - The name of the test case.

verb - An HTTP Method.

endpoint - The resource you want to invoke on the server.

host - Server host address.

headers - An HTTP Headers. All HTTP header fields are supported.

query_string - Query string parameters in the URL after the question mark.

payload - The data.

file are:

Best practices

reactions

One question may arise in your mind, how to add, structure and organize the test cases into my existing/new project. Every Python project, which has tests contains in his project directory a folder namely

tests/

reactions

From this directory by convention, great frameworks like

unittest

pytest

live_tests/

reactions

anddiscover and execute defined test cases in Python scripts. To not mess up with these tests and break the conventions, I suggest creating a new directory in your project root directory named

Inside the new directory, you can put all .

json

reactions

Tags

Subscribe to get your daily round-up of top tech stories!

files with defined test cases for the API. By doing this your tests will be easily distinguished. But it’s really up to you!