From the interface test beginner’s notes, write wrong place we give more advice oh

Many people use the Postman tool or are familiar with Python, but not necessarily with Python to write test cases. Postman can copy python code in its entirety.

(All the following content is based on the search function of Douban website as an example)

Convert postman interface use cases to Python test cases

Open Postman and click on the </> icon on the right. The script will appear on the right side of the page, and change the exported language at the top. Here I used Python-Reqyests

Copy the script and open it in PyCharm. If you don’t have the Reuqests library before importing it, an error may be reported.

Enter PIP install requests in the CMD command window

The exported script format is as follows:

import requests

url = "https://www.douban.com/search?"

payload={'q': 'three body'}
files=[

]
headers = {
  'Cookie': 'bid=5bBvkukAbvY'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
Copy the code

Convert to PyTest test case

1. Here is the test case converted to PyTest

import requests

class TestDouban:

    def test_douban(self) :
        url = "https://www.douban.com/search?"
        payload = {'q': 'three body'}
        files = []
        headers = {
          'Cookie': 'bid=5bBvkukAbvY'
        }
        response = requests.request("POST", url, headers=headers, data=payload, files=files)
        print(response.text)
Copy the code

Encapsulate the POST and GET methods

In a project, the path of the root route is the same, but the specific interface corresponding to different functions is different. In addition, POST and GET are common methods in current test cases. Therefore, you can encapsulate the root route, POST, and GET methods into a common class and call them directly later.

1.common.py – Common class encapsulation

import requests

class Common:
    def __init__(self) :
        # Douban root routing
        self.url_root = "https://www.douban.com"

    Params = params = params = params = params = params
    def get(self, uri, params=' ') :
        # patch access address
        url = self.url_root + uri + params
        Access the address via get
        response = requests.get(url)
        Return request's response, of type request's response type
        return response

    Params = params = params = params = params = params = params
    def post(self, uri, params=' ') :
        # patch access address
        url = self.url_root + uri
        If there are parameters, access the corresponding URL and assign the default parameter data
        if len(params) > 0:
            response = requests.post(url, data=params)
        No parameter, only need to access the corresponding URL
        else:
            response = requests.post(url)
        Return request's response, of type request's response type
        return response
Copy the code

2. Interface test cases

import requests

from common.common import Common

class TestDouban:
    def setup(self) :
        self.com = Common()

    def test_douban(self) :
        uri = "/search?"
        payload = {'q': 'three body'}
        response = self.com.post(uri, payload)
# delete file from hearder because file is not needed
Copy the code

The result is as follows: