当前位置:网站首页>See how Tencent does interface automation testing
See how Tencent does interface automation testing
2022-07-04 20:42:00 【Xiaowu knock code】
One 、 Automated classification
(1) Interface automation
python/java+requests+unittest Framework to implement python/java+RF(RobotFramework) Framework to implement —— The programming requirements are not high
(2)Web UI Function automation
python/java+selenium+unittest+ddt+PO Framework to implement python/java+RFS(RobotFrameWork+Selenium) Framework to implement —— The programming requirements are not high
(3)App automation
python/java+appnium+unittes Framework to implement python/java+RF(RobotFramework) Framework to implement —— The programming requirements are not high
Two 、 Interface automation and Web The difference between Automation
(1) Interface automation has no interface , There is no need to locate the interface elements , There is no need to consider the problem of interface delay , More efficient execution (2) Interface automation uses requests Test library ,Web For automation selenium Test library (3) The coverage of interface automation can reach 100%( Most interfaces can be automated ) Web The coverage of automation can reach 80-90% count OK( There may be some functions that cannot be automated )
3、 ... and 、 How to do interface automation
1、 technological process
A. Determine the scope of business , Which business function interfaces can be automated —— The coverage of interface automation can reach 100%
B. Time schedule , personnel allotment
C. Determine the automated test framework
D. Prepare the data —— Prepare interface use case data
E. Writing interface automation scripts
2、 Build interface automation test environment
(1)、 install python3.x—— To configure python Environment variables of
(2)、 install PyCharm——python development tool
(3)、 Install test library Requests library —— Provides a wealth of for sending requests , Processing requests API function xlrd,xlwt library —— Provide for the right to Excel File operation API function Pymysql library —— Provide for the right to Mysql Database operation API function paramsunittest library —— Realize parameterized Library Json library —— Provide for the right to Json Format data for operation API function (python Its own basic library ) Re library —— You can use API Function pair HMTL Data operation
3、 Prepare the data —— Prepare interface use case data
We put the interface use case data in Excel In the table , Because every interface contains : Request address , Request mode , Request parameters , And the response data ; So in Excel The table organizes our interface use case data in the following way , It includes the following contents : Use case name , Request address , Request mode , Request header , Request parameters , Expected results ( Assertion ) Then we will package a function to read Excel data , Pass it to the script in the form of parameters , The specific operation steps are as follows :
4、 Write automated test scripts
1、 step :
A、 Guide pack
import requests
B、 Organization request parameters
url = ‘http://localhost/fw/index.php?ctl=user&act=dologin&fhash=hbUjHVrQIgHkwdMdNGnPrSiIkVBeWcrOvJpmsXgyNuMewKfKGy’
par = {
‘email’: ‘Jason’,
‘user_pwd’: ‘TWlKaGRrRFJrQXJZZlFXYkh5WlNQZ2tpZkFZQlVlUUhyRE5SdndSUGdKanFDTG1LYUYlMjV1NjVCOSUyNXU3RUY0emdwMTIzNDU2JTI1dThGNkYlMjV1NEVGNg==‘,
‘auto_login’: 0,
‘ajax’: 1
}
C、 Send a request
res = requests.post(url, data=par)
res = requests.get(url,params=par)
D、 Extract the data in the response object , And make assertions
1、 Extract response *body* Content **
res.text —— If you return html Formatted data , Use res.text extract ` res.json() —— If the background returns json Formatted data , Use this API Function to extract `
2、 Extract the response header ***
res.headers
3、 Extract the status code , Response information
res.status_code
res.reason
4、 extract cookie value
res.cookies()
2、 Pass request header
header = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0’,
‘Accept’: ‘application/xml’,
}
res = requests.post(url,data=par,headers=header)
3、 Pass on cookie,token value
Pass through the request header —— View directly from the browser cookie value , And deliver it to the background .
header = {
‘Cookie’: ‘PHPSESSID=3724b412550a557825da5528dd6198c6’
}
res = requests.post(url,data=par,***headers=heade***r,allow_redirects=False)
Pass the request cookies This parameter is passed
import requests
#1. Sign in , obtain cookie value
def getCookie():
url = ‘http://localhost/fw/index.php?ctl=user&act=dologin&fhash=hbUjHVrQIgHkwdMdNGnPrSiIkVBeWcrOvJpmsXgyNuMewKfKGy’
par = {
‘email’: ‘Jason’,
‘user_pwd’: ‘TWlKaGRrRFJrQXJZZlFXYkh5WlNQZ2tpZkFZQlVlUUhyRE5SdndSUGdKanFDTG1LYUYlMjV1NjVCOSUyNXU3RUY0emdwMTIzNDU2JTI1dThGNkYlMjV1NEVGNg==‘,
‘auto_login’: 0,
‘ajax’: 1
}
res = requests.post(url, data=par)
return res.cookies
#2. Call the recharge interface
#2.1 obtain cookie value
cookie = getCookie()
#2.2 Send a recharge request
url = ‘http://localhost/fw/member.php?ctl=uc_money&act=incharge_done’
par = {
‘check_ol_bl_pay’:’on’,
‘money’:1000,
‘bank_id’:0,
‘memo’:234567890,
‘payment’:5,
}
# Send a recharge request
res1 = requests.post(url,data=par,cookies=cookie,allow_redirects=False) # Automatically redirected , You can cancel automatic redirection
print(res1.status_code)
print(res1.reason)
print(res1.headers)
So let's create one session object , All requests use this session Object to send
import requests
#1. Send login request
url = ‘http://localhost/fw/index.php?ctl=user&act=dologin&fhash=hbUjHVrQIgHkwdMdNGnPrSiIkVBeWcrOvJpmsXgyNuMewKfKGy’
par = {
‘email’: ‘Jason’,
‘user_pwd’: ‘TWlKaGRrRFJrQXJZZlFXYkh5WlNQZ2tpZkFZQlVlUUhyRE5SdndSUGdKanFDTG1LYUYlMjV1NjVCOSUyNXU3RUY0emdwMTIzNDU2JTI1dThGNkYlMjV1NEVGNg==‘,
‘auto_login’: 0,
‘ajax’: 1
}
# Create a seesion object , Use this later session Object to send a request
ses = requests.session()
# Send login request , Back to cookie Values are automatically saved to session In the object
response1 = ses.post(url,data=par)
#2. Send a recharge request
url = ‘http://localhost/fw/member.php?ctl=uc_money&act=incharge_done’
par = {
‘check_ol_bl_pay’:’on’,
‘money’:1000,
‘bank_id’:0,
‘memo’:234567890,
‘payment’:5,
}
response2 = ses.post(url,data=par,allow_redirects=False)
print(response2.headers)
5、 Project management, maintenance and optimization
(1)、 Data driven —— Realize the separation of interface use case data and script We put the interface use case data in Excel In the table , Because every interface contains : Request address , Request mode , Request parameters , And the response data ; So in Excel The table organizes our interface use case data in the following way , It includes the following contents : Use case name , Request address , Request mode , Request header , Request parameters , Expected results ( Assertion ) Then we will package a function to read Excel data , Pass it to the script in the form of parameters , The specific operation steps are as follows :
install xlrd library
pip install xlrd
call xlrd In the library API Function Excel Reading of table data
# Encapsulate a read Excel Functions of table data
# Yes Excel Reading table data requires a library ——xlrd library
import xlrd
def get_data(filename,sheetname):
#1. open Excel file
workbook = xlrd.open_workbook(filename)
#2. open Excel A table in the file
sheet = workbook.sheet_by_name(sheetname)
#3. Read the contents of the table
list = []
for I in range(1,sheet.nrows):
data = sheet.row_values(i)
list.append(data)
return list
if __name__==‘__main__’:
result = get_data(‘D:\\JMeter\\1947_Project\\cxy-project02\\data\\ Interface use case data .xls’,’ Sign in ’)
print(result)
Problem solving 1
Engineering problems :
1、 No installation xlrd
2、 Didn't put xlrd Import the project
(2)、unittest frame effect : Used to manage use cases , Load use cases , Execute use cases principle : There are several core components 1、 Test firmware setUp() Before each use case is executed , The first thing to do is setUp() Method , stay setUp() Method such as : Connect to database , Later, we will Web UI When functions are automated , You can open the browser here , To configure tearDown() After each use case is executed , Recycle some resources , such as : Close the database , Close the browser 2、 The test case Each use case needs to implement a use case method , Every use case method must be expressed in test start 3、 test suite When executing use cases , You need to create a test suite , Add use cases to the test suite . 4、 loader Used to load use cases , Add test cases to the test suite 5、 actuator Used to execute the use cases in the test suite How to use unittest Framework to write use cases
#1. Guide pack
import time
import unittest
import requests
from common.excelUtils import get_data
import paramunittest
# Read excel Data in the table
list = get_data(‘D:\\JMeter\\1947_Project\\cxy-project02\\data\\ Interface use case data .xls’,’ Sign in ’)
#2. Define a class , To inherit unittest.TestCase
@paramunittest.parametrized(*list) # quote list All data in
class FwLogin(unittest.TestCase):
def setParameters(self,case_name,url,method,headers,params,assert_info):
‘’’
How many use cases , How many times will this function be executed , This function will be executed before each use case , Extract the data .
:param case_name:
:param url:
:param method:
:param headers:
:param params:
:param assert_info:
:return:
‘’’
self.case_name = str(case_name)
self.url = str(url)
self.method = str(method)
self.headers = str(headers)
self.params = str(params)
self.assert_info = str(assert_info)
#1. Implement a use case method
def test_login_case(self):
time.sleep(5)
#1. Organizational parameters
self.headers= eval(self.headers) # Convert strings to dictionaries
self.params = eval(self.params)
#2. Send the request
if self.method == ‘POST’:
response = requests.post(self.url,data=self.params,headers=self.headers)
else:
response = requests.get(self.url,params=self.params,headers=self.headers)
#3. Check , Assertion
self.check_result(response)
def check_result(self,response):
‘’’
Assertion Of inspection results
:param response:
:return:
‘’’
self.assert_info = eval(self.assert_info) # Expected results
try:
self.assertEqual(response.status_code,200,’ Response status code error ’)
self.assertEqual(response.reason,’OK’,’ The response code of the response is wrong ’)
self.assertDictEqual(response.json(),self.assert_info,’ The body content of the response is inconsistent !’)
print(‘%s The test case passed !’ %self.case_name)
except AssertionError as e:
print(‘%s The test case fails !%s’ %(self.case_name,e))
if __name__ == ‘__main__’:
unittest.main()
Finally, thank everyone who reads my article carefully , The following online link is also a very comprehensive one that I spent a few days sorting out , I hope it can also help you in need !

These materials , For those who want to change careers 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful ……
If you don't want to grow up alone , Unable to find the information of the system , The problem is not helped , If you insist on giving up after a few days , You can click the small card below to join our group , We can discuss and exchange , There will be various software testing materials and technical exchanges .
| Click the small card at the end of the document to receive it |
Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .
Self study recommendation B Stop video :
Zero basis transition software testing :25 Days from zero basis to software testing post , I finished today , Employment tomorrow .【 Include features / Interface / automation /python automated testing / performance / Test Development 】
Advanced automation testing :2022B The first station is super detailed python Practical course of automated software testing , Prepare for the golden, silver and four job hopping season , After advanced learning, it soared 20K
边栏推荐
- 左右最值最大差问题
- On communication bus arbitration mechanism and network flow control from the perspective of real-time application
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
- Informatics Olympiad 1336: [example 3-1] find roots and children
- What financial products can you buy with a deposit of 100000 yuan?
- jekins初始化密码没有或找不到
- LeetCode 871. 最低加油次数
- Function analysis and source code of hash guessing game system development
- Prometheus installation
猜你喜欢

【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt

什么叫内卷?

科普达人丨一文看懂阿里云的秘密武器“神龙架构”

如何让你的小游戏适配不同尺寸的手机屏幕

What is involution?
![NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
![[Beijing Xunwei] i.mx6ull development board porting Debian file system](/img/46/abceaebd8fec2370beec958849de18.jpg)
[Beijing Xunwei] i.mx6ull development board porting Debian file system

FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC

Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法

电脑怎么保存网页到桌面上使用
随机推荐
node强缓存和协商缓存实战示例
Is it necessary to apply for code signing certificate for software client digital signature?
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
左右最值最大差问题
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
Understand the reading, writing and creation of files in go language
Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
MySQL中的日期时间类型与格式化方式
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
Process of manually encrypt the mass-producing firmware and programming ESP devices
Length of the longest integrable subarray
泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
mysql语句执行详解
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
【深度学习】一文看尽Pytorch之十九种损失函数
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
Cdga | six principles that data governance has to adhere to