当前位置:网站首页>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
边栏推荐
- Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
- What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
- Flet教程之 04 FilledTonalButton基础入门(教程含源码)
- 2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- 一文搞懂Go语言中文件的读写与创建
- What financial products can you buy with a deposit of 100000 yuan?
- What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
- 精选综述 | 用于白内障分级/分类的机器学习技术
- 电脑怎么保存网页到桌面上使用
猜你喜欢
B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
What if the brightness of win11 is locked? Solution to win11 brightness locking
工厂从自动化到数字孪生,图扑能干什么?
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
[Beijing Xunwei] i.mx6ull development board porting Debian file system
精选综述 | 用于白内障分级/分类的机器学习技术
Jiuqi ny8b062d MCU specification /datasheet
如何让你的小游戏适配不同尺寸的手机屏幕
idea配置标准注释
随机推荐
输入的查询SQL语句,是如何执行的?
Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
jekins初始化密码没有或找不到
node强缓存和协商缓存实战示例
Jiuqi ny8b062d MCU specification /datasheet
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
Flet教程之 05 OutlinedButton基础入门(教程含源码)
Practical examples of node strong cache and negotiation cache
Template_ Large integer subtraction_ Regardless of size
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
js 闭包
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
实践示例理解js强缓存协商缓存
Employment prospects and current situation of Internet of things application technology
Template_ Judging prime_ Square root / six prime method
Is it necessary to apply for code signing certificate for software client digital signature?
2022 Health Exhibition, health exhibition, Beijing Great Health Exhibition and health industry exhibition were held in November