Have a comprehensive and in-depth understanding of interface automation, and I will report the address after reading it

1. Automatic classification

(1) Interface automation

python/java+requests+unittest framework to implement python/java+RF (RobotFramework) framework to achieve - not high requirements for programming

(2) Web UI function automation

python/java+selenium+unittest+ddt+PO framework to implement python/java+RFS (RobotFrameWork+Selenium) framework to achieve - not high requirements for programming

(3) App automation

python/java+appnium+unittes framework to implement python/java+RF (RobotFramework) framework to achieve - not high requirements for programming

2. The difference between interface automation and Web automation

(1) Interface automation has no interface, does not need to locate the interface elements, does not need to consider the problem of interface delay, and has higher execution efficiency

(2) The requests test library is used for interface automation, and the selenium test library is used for Web automation

(3) The coverage rate of interface automation can reach 100% (most interfaces can be automated) The coverage rate of Web automation can reach 80-90%, which is OK (there may be some functions that cannot be automated)

3. How to do interface automation

3.1. Process

A. Determine the business scope, which business function interfaces can be automated - the coverage rate of interface automation can reach 100%

B. Time schedule, personnel allocation

C. Determine the automation testing framework

D. Prepare data - prepare interface use case data

E. Write Interface Automation Scripts

3.2. Build an interface automation test environment

1. Install python3.x - configure python environment variables

2. Install PyCharm - python development tool

3. Install the test library:

Requests library - provides a wealth of API functions for sending and processing requests

xlrd, xlwt library - provides API functions to operate on Excel files

Pymysql library - provides API functions to operate on Mysql database

paramsunittest library - a library that implements parameterization

Json library - provides API functions to operate on data in Json format

(Python's own basic library) Re library - you can use the API functions in this library to operate on HMTL data

3.3. Prepare data

Prepare the interface use case data We put the interface use case data into an Excel sheet, because each interface includes: request address, request method, request parameters, and response data; so organize our interface use cases in the Excel sheet in the following way Data, including the following contents: use case name, request address, request method, request header, request parameters, expected result (assertion) Then we will encapsulate a function to read Excel data and pass it to the script as parameters, specifically The operation steps are as follows:

3.4. Writing automated test scripts

 
1、步骤:
    A、导包
import requests
    B、组织请求参数
url = ‘http://localhost/fw/index.php?ctl=user&act=dologin&fhash=hbUjHVrQIgHkwdMdNGnPrSiIkVBeWcrOvJpmsXgyNuMewKfKGy’
par = {   
         ‘email’: ‘Jason’,   
         ‘user_pwd’: ‘TWlKaGRrRFJrQXJZZlFXYkh5WlNQZ2tpZkFZQlVlUUhyRE5SdndSUGdKanFDTG1LYUYlMjV1NjVCOSUyNXU3RUY0emdwMTIzNDU2JTI1dThGNkYlMjV1NEVGNg==‘,    
        ‘auto_login’: 0,   
        ‘ajax’: 1
}
    C、发送请求
res = requests.post(url, data=par)
res = requests.get(url,params=par)
    D、提取响应对象中的数据,并做断言
1、提取响应*body*内容**
     res.text     —— 如果返回的是html格式的数据,使用res.text提取`
     res.json()   —— 如果后台返回的是json格式的数据,则使用这个API函数来提取`
2、提取响应头***
     res.headers
3、提取状态码,响应信息
     res.status_code
     res.reason
4、提取cookie值
     res.cookies()
2、传递请求头
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、传递cookie,token值
    通过请求头来传递 —— 直接从浏览器上查看cookie值,并传递到后台。
header = {
    ‘Cookie’: ‘PHPSESSID=3724b412550a557825da5528dd6198c6’                  
}
res = requests.post(url,data=par,***headers=heade***r,allow_redirects=False) 
    发请求的时候通过cookies这个参数来传递
import requests
#1. 登录,获取cookie值
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. 调用充值接口
#2.1 获取cookie值
cookie = getCookie()
#2.2 发充值请求
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,
}
#发充值请求
res1 = requests.post(url,data=par,cookies=cookie,allow_redirects=False)            # 自动重定向的,可以取消自动重定向
print(res1.status_code)
print(res1.reason)
print(res1.headers)
    先创建一个session对象,所有请求都使用这个session对象来发送
import requests
#1. 发登录请求
url = ‘http://localhost/fw/index.php?ctl=user&act=dologin&fhash=hbUjHVrQIgHkwdMdNGnPrSiIkVBeWcrOvJpmsXgyNuMewKfKGy’
par = {
    ‘email’: ‘Jason’,
    ‘user_pwd’: ‘TWlKaGRrRFJrQXJZZlFXYkh5WlNQZ2tpZkFZQlVlUUhyRE5SdndSUGdKanFDTG1LYUYlMjV1NjVCOSUyNXU3RUY0emdwMTIzNDU2JTI1dThGNkYlMjV1NEVGNg==‘,
    ‘auto_login’: 0,
    ‘ajax’: 1
}
#创建一个seesion对象,后期使用这个session对象来发请求
ses = requests.session()
#发登录请求,返回的cookie值会自动化保存到session对象中
response1 = ses.post(url,data=par)
#2. 发充值请求
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)

3.5. Project management maintenance and optimization

1. Data-driven——to realize the separation of interface use case data and scripts. We put the interface use case data in an Excel sheet, because each interface includes: request address, request method, request parameters, and response data; so in the Excel sheet Organize our interface use case data in the following way, including the following contents: use case name, request address, request method, request header, request parameters, expected result (assertion) and then we will encapsulate a function to read Excel data, It is passed to the script as a parameter, and the specific operation steps are as follows:

    安装xlrd库
    pip install xlrd
    调用xlrd库中的API函数来实现对Excel表格数据的读取
#封装一个读取Excel表格数据的函数
#对Excel表格数据的读取需要用到一个库——xlrd库
import xlrd
def get_data(filename,sheetname):
    #1. 打开Excel文件
    workbook = xlrd.open_workbook(filename)    
    #2. 打开Excel文件中的某张表
    sheet = workbook.sheet_by_name(sheetname)   
    #3. 读取表中的内容
    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\\接口用例数据.xls’,’登录’)
    print(result)
    问题解决1
工程问题:
    1、没有安装xlrd
    2、没有把xlrd导入工程

2. unittest framework

Role: 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 setUp() method will be executed first, and the preparation and initialization work will be completed in the setUp() method, such as: connecting to the database. Later, when automating the Web UI function, you can click here To open the browser, configure tearDown() After each use case is executed, recycle some resources, such as: close the database, close the browser

2. Test cases Each use case needs to implement a use case method, and each use case method must start with test

3. When the test suite executes the use case, it is necessary to create a test suite and add the use case to the test suite.

4. The loader is used to load use cases, and add test cases to the test suite

5. The executor is used to execute the use cases in the test suite. How to use the unittest framework to write use cases

#1. 导包
import time
import unittest
import requests
from common.excelUtils import get_data
import paramunittest
#读取excel表格中的数据
list = get_data(‘D:\\JMeter\\1947_Project\\cxy-project02\\data\\接口用例数据.xls’,’登录’)
#2. 定义一个类,去继承unittest.TestCase
@paramunittest.parametrized(*list)                  # 引用list中的所有数据
class FwLogin(unittest.TestCase):
    def setParameters(self,case_name,url,method,headers,params,assert_info):
        ‘’’
        有多少条用例,这个函数就会执行多少次,每执行一条用例之前先会执行这个函数,把数据提取出来。
        :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. 实现一个用例方法
    def test_login_case(self):
        time.sleep(5)
        #1. 组织参数
        self.headers= eval(self.headers)                # 将字符串转化为字典
        self.params = eval(self.params)
        #2. 发请求
        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. 检查,断言
        self.check_result(response)
    def check_result(self,response):
        ‘’’
        断言  检查结果的
        :param response:
        :return:
        ‘’’
        self.assert_info = eval(self.assert_info)           #预期结果
        try:
            self.assertEqual(response.status_code,200,’响应状态码错误’)
            self.assertEqual(response.reason,’OK’,’响应的响应码错误’)
            self.assertDictEqual(response.json(),self.assert_info,’响应的正文内容不一致!’)
            print(‘%s测试用例通过!’ %self.case_name)
        except AssertionError as e:
            print(‘%s测试用例不通过!%s’ %(self.case_name,e))
 
if __name__ == ‘__main__’:
    unittest.main()

be careful

Finally, basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, packet capture tool topics, interface testing tools, advanced testing-Python programming, Web automated testing, APP automated testing, interface automated testing, testing advanced continuous integration, Supporting learning resources such as test framework development test framework, performance test, security test [free].

Don't just be a person whose collections never stop and actions never start. Many things can be learned without a teacher. If you can think a little more in the process of doing it, and look at other people's experience and practices, you will grow faster and the effect will be better! Come on, testers! The road is at your feet, success is tomorrow!

 

Guess you like

Origin blog.csdn.net/HUA1211/article/details/130011793