Testing REST APIs easily in Python with pyhttptest

Written by slaily | Published 2019/12/12
Tech Story Tags: python | programming | software-testing | unit-testing | python-web-development | api | restful-apis | latest-tech-stories

TLDR The tool is a command-line tool for HTTP tests over RESTful APIs. Describe your HTTP Requests test cases against your API service in a simplest and widely used format. The properties, which you can pass to a file are:name - The name of the test case.verb - An HTTP Method. The endpoint - The resource you want to invoke on the server. The report from the single test case and the report from multiple test cases. The test cases will be easily distinguished.via the TL;DR App

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.
pyhttptest - A command-line tool for HTTP tests over RESTful APIs 
This tool automate testing in a simple three steps.
1. Install the package
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.
Single test case definition examples 
  • 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"
        }
      }
      
  • 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!"
        }
      }
      
Multiple test cases definition example
  • 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
pyhttptest execute data/test_server_status.json
The report from the single test case
pyhttptest execute data/requests.json
The report from the multiple test cases
The properties, which you can pass to a .
json
file are:
  • 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.
Best practices
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/
.
From this directory by convention, great frameworks like
unittest
and
pytest
discover 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
live_tests/
.
Inside the new directory, you can put all .
json
files with defined test cases for the API. By doing this your tests will be easily distinguished. But it’s really up to you!

Published by HackerNoon on 2019/12/12