Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to easily test REST API with pyhttptest

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use pyhttptest to easily test REST API, for this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Now, each of us is faced with REST API, either develop such a service or use such a service. In addition, we are in the popular era of micro-services, where we divide our business logic into small, independent services that are independent of each service. Most of these services follow RESTful principles and use the JSON format for communication, which is the most widely used format because of its simplicity.

Pyhttptest command line tool for HTTP testing through RESTful api.

This tool automates testing in three simple steps

Installation

Pip install pyhttptest

Describe HTTP request test cases for API services in JSON, the simplest and most widely used format in the file

Send HTTP GET request json file as follows: ```json {"name": "TEST: Get server status", "verb": "GET", "endpoint": "/ get", "host": "https://httpbin.org"," headers ": {" Accept-Language ":" en-US "}}

-

-send HTTP POST request

The content of json file is as follows

```json

{

"name": "TEST: Create an HTML bin"

"verb": "POST"

"endpoint": "post"

"host": "https://httpbin.org","

"payload": {

Content: "Hello, world!"

}

}

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": "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/""

}

]

Run the command and get the report

Pyhttptest execute data/filename.json

Report screenshots from test cases

Properties of test cases

Name-name of the test case

Verb-HTTP method

Endpoint-Resource to be invoked on the server

Host-Server host addr

Headers-A HTTP header. All HTTP headers

Query_string-query string-query string parameter in the URL after the question mark

Payload-data

Tips

You may have the question of how to add, structure, and organize test cases into my existing / new project. For each Python project, there are tests contained in his project directory in a folder called tests/.

From this directory, by convention, great frameworks such as unittest and and pytest discover and execute the test cases defined in Python scripts. In order not to mess up these tests and break the rules, I recommend creating a live_tests/ in the root directory of your project.

In the new directory, you can take all the json files and define the files for the API test case. By doing so, your tests will be easy to distinguish.

This is the answer to the question about how to use pyhttptest to easily test REST API. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report