We all know the interface test , So what are the test methods , We have common tools postman,jmeter etc. . These provide very powerful functions , But there are also code based tests , That's it python requests, If you have basic programming skills , You can think about this , The advantage is that it can be automated , Lower maintenance costs . Then this article also introduces you to the basic methods of use .
So since it's interface testing , Then you need an interface , This public interface is OK api, For example, baidu translation interface , WeChat official account interface , Wechat login interface , Alipay payment interface, etc. , Of course, it can also be a website built by yourself , So this time we will take the simplest one I wrote as our test interface , Specifically python requests Usage of , How to write the interface , Let's use python Medium flask Framework to write a very simple code , You need to install one before writing flask modular , Specific and direct pip install flask That's all right. . So what is the effect of writing it out , Let's look at the code
from flask import Flask,request
app = Flask(__name__) # ,template_folder=
app.debug = True # Automatic restart
# Routing function , Change the function that the function executes , It is understood that the input address executes the function of the corresponding address
@app.route("/", methods=['GET', 'POST']) # method Plus other methods , Otherwise, only get
def go():
if request.method=="GET":
return {"code": "success"}
elif request.method=="POST":
# obtain post Transmitted value , Then make a comparison
if request.form.get("user")=="xiaoli" and request.form.get("pwd")=="123456":
return {"code": "success"}
else:
return {"code": "ERROR"}
# Start the server
if __name__ == '__main__':
app.run()
If you don't understand , You can see flask Some knowledge of the framework , I won't talk too much about it here , You can also copy the above code , Just run it directly , You can treat this machine as a server ,url yes http://127.0.0.1:5000/. We finished writing the server side , Let's start writing python requests The specific code of , as follows
# Import required requests modular
import requests
# ordinary get request , use get Method
res = requests.get("http://127.0.0.1:5000/")
# print(res.text)
# Get the response amount result
print(res.content.decode("utf-8"))
The results are shown in the following figure
The above is a simple get request , The returned data indicates that it succeeded , There is no problem with the interface , Let's execute post The request code is as follows
import requests
url = "http://127.0.0.1:5000/"
data = {"user": "xiaoli",
"pwd":"123456" }
# ordinary get request
# res = requests.get("http://127.0.0.1:5000/")
# print(res.text)
# perform post request , Parameter is url and body data , If there are special header parameters, add headers=“”
res = requests.post(url=url, data=data)
print(res.content.decode("utf-8"))
The following result is post Successful results , The user name and password are consistent .
The data we transmitted above is correct , Let's pass on the wrong , See how the code reacts , The data user Change to xiaoli12, See how you react
import requests
url = "http://127.0.0.1:5000/"
# user Change to xiaoli123
data = {"user": "xiaoli123",
"pwd":"123456" }
# ordinary get request
# res = requests.get("http://127.0.0.1:5000/")
# print(res.text)
res = requests.post(url=url, data=data)
print(res.content.decode("utf-8"))
The results are shown below
This is also the same as the expected result , No problem , Keep users unique ,
The above is our requests Basic usage , Later, we will continue to update the advanced usage , Such as automation , Well, that's all for the article