paint-brush
Testing REST APIs easily in Python with pyhttptest by@slaily
8,453 reads
8,453 reads

Testing REST APIs easily in Python with pyhttptest

by IliyanDecember 12th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

Company Mentioned

Mention Thumbnail
featured image - Testing REST APIs easily in Python with pyhttptest
Iliyan HackerNoon profile picture
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.

- 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": "//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": "//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": "//localhost:8085/",
    "headers": {
      "Accept-Language": "en-US"
    },
    "query_string": {
      "limit": 1
    }
  },
  {
    "name": "TEST: Add a new user",
    "verb": "POST",
    "endpoint": "api/v1/users",
    "host": "//localhost:8085/",
    "payload": {
      "username": "pyhttptest",
      "email": "[email protected]"
    }
  },
  {
    "name": "TEST: Modify an existing user",
    "verb": "PUT",
    "endpoint": "api/v1/users/XeEsscGqweEttXsgY",
    "host": "//localhost:8085/",
    "payload": {
      "username": "pyhttptest"
    }
  },
  {
    "name": "TEST: Delete an existing user",
    "verb": "DELETE",
    "endpoint": "api/v1/users/XeEsscGqweEttXsgY",
    "host": "//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 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!

바카라사이트 바카라사이트 온라인바카라