当前位置:网站首页>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
边栏推荐
- Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
- Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
- In operation (i.e. included in) usage of SSRs filter
- 输入的查询SQL语句,是如何执行的?
- 什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
- 左右最值最大差问题
- 太方便了,钉钉上就可完成代码发布审批啦!
- 漫谈客户端存储技术之Cookie篇
- Flet教程之 04 FilledTonalButton基础入门(教程含源码)
- Stack: how to realize the judgment of valid brackets?
猜你喜欢
[in-depth learning] review pytoch's 19 loss functions
Flet教程之 06 TextButton基础入门(教程含源码)
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
Process of manually encrypt the mass-producing firmware and programming ESP devices
Practice examples to understand JS strong cache negotiation cache
Flet教程之 08 AppBar工具栏基础入门(教程含源码)
工厂从自动化到数字孪生,图扑能干什么?
How does the computer save web pages to the desktop for use
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
Qt五子棋人机对战画棋子之QPainter的使用误区总结
随机推荐
记录线上bug解决list(未完待续7/4)
六石编程学:关于代码,有六个得意
What financial products can you buy with a deposit of 100000 yuan?
LeetCode 871. 最低加油次数
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
最长的可整合子数组的长度
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
漫谈客户端存储技术之Cookie篇
软件客户端数字签名一定要申请代码签名证书吗?
实践示例理解js强缓存协商缓存
哈希(Hash)竞猜游戏系统开发功能分析及源码
长城证券开户安全吗 股票开户流程网上开户
一文搞懂Go语言中文件的读写与创建
C # better operation mongodb database
NetCore3.1 Json web token 中间件
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
CDGA|数据治理不得不坚持的六个原则
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
语义化标签的优势和块级行内元素
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system