当前位置:网站首页>Interface Automation Testing Basics

Interface Automation Testing Basics

2022-08-10 13:19:00 Jerry Lee~

I. Common methods of understanding the requests library

The third-party library for python: pip install requests

Knowledge point: pytest's default test case rules

1. The module name must end with test_ or _test.

2. The class name must start with Test

3. The test case name must start with test_

post,delete,put,getAdd, delete, modify, query restful

def get(url, params=None, **kwargs): #Send get requestdef post(url, data=None, json=None, **kwargs): #Send post requestdata is a form parameterjson is json data parameterdef put(url, data=None, **kwargs): #Send put requestdef delete(url, **kwargs): #Send delete request----------------------The first four methods will call the following methods-------------------------def requests.request(method, url, **kwargs): #Send the request according to the request method of the method passing parameters----------------------The previous method calls the following request method-----------------------def session.request(self,method,url,params=None,data=None,headers=None,cookies=None,files=None,auth=None,timeout=None,allow_redirects=True,proxies=None,hooks=None,stream=None,verify=None,cert=None,json=None,):---------------------------------------------------------------------------------def session(): #Get the session object

method: request method

url: request path

params: get request parameters

data=None can be a post or put parameter

json=None The second way to pass parameters for post requests

headers=None Request headers

cookies=None cookie information in the request header

files=Nnoe file upload

stream=None File Download

Second, common methods of understanding response

  1. res.txt Return String
  2. res.content Returns byte data
  3. res.json() Returns json data, python is a dictionary
  4. res.status_code Returns the status code
  5. res.reason Returns status information
  6. res.cookies Returns the response cookie
  7. res.encoding Returns the response encoding
  8. res.headers Returns response headers
  9. res.request.xxx returns some requested data

Three, actual combat

get request passes parameters through params

The post request passes parameters through data and json

1. Form file transfer parameters: Content-Type: form-data (multipart/form-data), which supports uploading files FormType;

2. Form parameters: Content-Type: x-www-form-urlencoded, interface request of form type;

3. Text raw parameters: json: Content-Type: application/json (supports various native types, JSON type interface requests);

4, binary binary parameter: Content-Type: Content-Type: application/octet-stream (binary, stream type interface request): Corresponding to Content-Type: application/octet-stream in the http request, onlyBinary data can be uploaded, usually used to upload files. Since there is no key value, only one file can be uploaded at a time;

Fourth, authentication (cookie authentication, session authentication and token authentication)

Identify whether you have permission to access the interface.

Both sessions and tokens can be transmitted through cookies.

Principle

cookies: stored in the browser, with size limit and status;

session: stored in the server, the server has resource overhead, distributed and cross-system is not easy to achieve;

Token: The client can save the Token anywhere, unlimited and stateless, which is conducive to distributed deployment.

原网站

版权声明
本文为[Jerry Lee~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101229061452.html