当前位置:网站首页>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
边栏推荐
- Template_ Large integer subtraction_ Regardless of size
- 如何让你的小游戏适配不同尺寸的手机屏幕
- What ppt writing skills does the classic "pyramid principle" teach us?
- 左右最值最大差问题
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Qt五子棋人机对战画棋子之QPainter的使用误区总结
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- 漫谈客户端存储技术之Cookie篇
- How is the entered query SQL statement executed?
- Is it necessary to apply for code signing certificate for software client digital signature?
猜你喜欢
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
九齐NY8B062D MCU规格书/datasheet
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
实操自动生成接口自动化测试用例
AP8022开关电源小家电ACDC芯片离线式开关电源IC
【深度学习】一文看尽Pytorch之十九种损失函数
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
随机推荐
华为云云商店首页 Banner 资源位申请
PHP pseudo original API docking method
漫谈客户端存储技术之Cookie篇
idea插件
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
Template_ Large integer subtraction_ Regardless of size
Six stones programming: about code, there are six triumphs
左右最值最大差问题
浏览器渲染页面过程
Flet教程之 07 PopupMenuButton基础入门(教程含源码)
Employment prospects of neural network Internet of things application technology [welcome to add]
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
What does the neural network Internet of things mean? Popular explanation
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
什么叫内卷?
剑指 Offer II 80-100(持续更新)
托管式服务网络:云原生时代的应用体系架构进化
In operation (i.e. included in) usage of SSRs filter
On communication bus arbitration mechanism and network flow control from the perspective of real-time application