当前位置:网站首页>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
边栏推荐
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- What should I do if my computer sharing printer refuses access
- 精选综述 | 用于白内障分级/分类的机器学习技术
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
- Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
- 【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
- What does the neural network Internet of things mean? Popular explanation
猜你喜欢
什么叫内卷?
Practical examples of node strong cache and negotiation cache
What should I do if my computer sharing printer refuses access
Selected review | machine learning technology for Cataract Classification / classification
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Jiuqi ny8b062d MCU specification /datasheet
同事的接口文档我每次看着就头大,毛病多多。。。
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
工厂从自动化到数字孪生,图扑能干什么?
随机推荐
What should I do if my computer sharing printer refuses access
What does the neural network Internet of things mean? Popular explanation
Template_ Large integer subtraction_ Regardless of size
奏响青春的乐章
栈:如何实现有效括号的判断?
电脑怎么保存网页到桌面上使用
What is the development of block hash quiz game system? Hash quiz game system development (case mature)
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
NetCore3.1 Json web token 中间件
The problem of the maximum difference between the left and right maxima
How to adapt your games to different sizes of mobile screen
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
Huawei cloud store homepage banner resource bit application
Dynamic memory management
word中插入图片后,图片上方有一空行,且删除后布局变乱
Jiuqi ny8b062d MCU specification /datasheet
Detailed explanation of Audi EDI invoice message
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
华为云云商店首页 Banner 资源位申请