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)
Open Postman and click the </> icon on the right. The script will be displayed on the right side of the page, and the exported language will be changed 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 = “www.douban.com/search?”
Payload ={‘q’: ‘x ‘} files=[
] headers = { ‘Cookie’: ‘bid=5bBvkukAbvY’ }
response = requests.request(“POST”, url, headers=headers, data=payload, files=files)
print(response.text)
1. Here are the test cases converted to PyTest
import requests
class TestDouban:
Def test_douban (self) : url = "https://www.douban.com/search?" > content = {' q: 'three body'} files = [] headers = {' cookies' : 'bid=5bBvkukAbvY' } response = requests.request("POST", url, headers=headers, data=payload, files=files) print(response.text)Copy the code
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, so you can encapsulate the root route, POST and GET methods into a common class, which can be called directly later.
1.common.py – Common class encapsulation
import requests
Class Common: def init(self): # self.url_root = “www.douban.com”
Def get(self, uri, params= "") def get(self, uri, params=" ") Url = self.url_root + uri + params Def post(self, uri, params= "") def post(self, uri, params=" "): If len(params) > 0: if len(params) > 0: if len(params) > 0 Response = requests. Post (url, data=params) Response = requests. Post (URL) # return requests for responseCopy 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?" Self.com.post (uri, payload)Copy the code
# delete file from hearder because file is not needed
The result is as follows:
These materials should be the most comprehensive and complete preparation warehouse for my friend [Software Testing]. This warehouse also accompanied me through the most difficult journey. I hope it can also help you
Follow my wechat public account [sad Latiao] for free ~
Send a word: the appearance of the world depends on your gaze at its vision, their value depends on your pursuit and mentality, all good wishes, not in the waiting to have, but in the struggle for.
My learning exchange group: 902061117 group of technical cattle to communicate and share ~
If my blog is helpful to you, if you like my blog content, please “like” “comment” “favorites” one key three even oh!