python - Interface test automation combat - case1 - optimized version

Title: 
  complete automated testing based on the following two interfaces and data interfaces, and generate test reports:
'''
登录

login='http://47.107.168.87:8080/futureloan/mvc/api/member/login'
login_data={'mobilephone':18688773467,'pwd':'123456'}

充值

recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'
recharge_data={'mobilephone':18688773467,'amount':'1000'}
'''
test_excel.xlsx:


http_request.py
Copy the code
# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: http_request.py
@ide: PyCharm Community Edition
@time: 2018-12-05 10:06
@blog: https://www.cnblogs.com/gotesting/

'''

import requests

class HttpRequest:

    def http_request(self,url,param,method,cookies=None):
        if method == 'get':
            res = requests.get(url,param,cookies = cookies)
        elif method == 'post':
            res = requests.post(url,param,cookies = cookies)
        else:
            print('请求方法错误!')
        return res
Copy the code
 
read_excel.py
Copy the code
# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: read_excel.py
@ide: PyCharm Community Edition
@time: 2018-12-05 11:57
@blog: https://www.cnblogs.com/gotesting/

'''

from openpyxl import load_workbook

class ReadExcel:

    def read_excel(self):
        wb = load_workbook('test_excel.xlsx')
        sheet = wb['登录及充值测试数据']
        test_data = []
        for i in range(2,sheet.max_row+1):
            sub_data = {}
            sub_data['url'] = sheet.cell(i,1).value
            sub_data['param'] = eval(sheet.cell(i,2).value)
            sub_data['method'] = sheet.cell(i,3).value
            sub_data['expected'] = sheet.cell(i,4).value
            test_data.append(sub_data)
        return test_data
Copy the code

 



test_api.py
Copy the code
# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: TestApi.py
@ide: PyCharm Community Edition
@time: 2018-12-05 10:09
@blog: https://www.cnblogs.com/gotesting/

'''

import unittest

from TestApi.http_request import HttpRequest

cookies = None

class TestHttpApi(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    # 改写__init__方法,使用超继承
    def __init__(self,url,param,method,expected,methodName):
        self.url = url
        self.param = param
        self.method = method
        self.expected = expected
        super(TestHttpApi,self).__init__(methodName)

    def test_api(self):
        global cookies
        res = HttpRequest().http_request(self.url,self.param,self.method,cookies)
        print('请求结果:',res.json())
        if res.cookies:
            cookies = res.cookies

        try:
            self.assertEquals(self.expected,res.json()['msg'])
        except AssertionError as e:
            print('断言异常:',e)
            raise e
Copy the code

 

test_suite.py
Copy the code
# - * - Coding: UTF-. 8 - * - 

'' ' 
@project: jiaxy 
@author: Jimmy 
@file: test_suite.py 
@ide: PyCharm Community Community Edition 
@time: 2018-12-05 10:28 
@blog: HTTPS : //www.cnblogs.com/gotesting/ 

'' ' 

Import the unittest 
Import HTMLTestRunner 
from TestApi.test_api Import TestHttpApi 
from TestApi.read_excel Import ReadExcel 

# define the test data list 
TEST_DATA = ReadExcel () read_excel (). 
Print (TEST_DATA) 
' ' '  
TEST_DATA = [{' URL ':' HTTP: //47.107.168.87: 8080 / futureloan / MVC / API / Member / Login ',' param ': {' MobilePhone, ':' ',' pwd ':' 123456 ' }, 'method': 'get ', 'expected': ' phone number can not be empty'},
             {' URL ':' HTTP: //47.107.168.87: 8080 / futureloan / MVC / API / Member / Login ',' param ': {' MobilePhone, ': 18,688,773,467,' pwd ':' '},' Method ' : 'get', 'expected' : ' password can not be empty'},
             { 'url': 'http: //47.107.168.87: 8080 / futureloan / mvc / api / member / login', 'param': { 'mobilephone': 18688773467, 'pwd': '12345678'}, 'method' : 'get', 'expected' : ' a user name or password is incorrect'}, 
             { 'URL': 'HTTP: //47.107.168.87: 8080 / futureloan / MVC / API / Member / Login', 'param': { 'mobilephone': 18688773467, 'pwd ': '123456'}, 'method': 'get', 'expected': ' successful login'},  
             { 'url': 'http: //47.107.168.87 :8080 / futureloan / mvc / api / member / recharge ',' param ': {' mobilephone ':' ',' amount ':' 1000 '},' method ':' post ',' expected ':' phone number can not be Is empty'},
             { 'URL': 'HTTP: //47.107.168.87: 8080 / futureloan / mvc / api / member / recharge ',' param ': {' mobilephone ': 1868877346,' amount ':' 1000 '},' method ':' post ',' expected ':' phone number format is not correct '} , 
             { 'URL': 'HTTP: //47.107.168.87:8080 / futureloan / mvc / api / member / recharge ',' param ': {' mobilephone ': 18688773467,' amount ':' '},' method ':' post ',' expected ':' Please Enter Amount '} ,
             { 'url': 'http: //47.107.168.87: 8080 / futureloan / mvc / api / member / recharge', 'param': { 'mobilephone': 18688773467, 'amount': '100000000000'}, 'method' : 'post', 'expected' : ' Please enter a positive amount range between 0 and 500,000'}, 
             { 'URL': 'HTTP: //47.107.168.87: 8080 / futureloan / MVC / API / Member / recharge ',' param ': {' mobilephone ': 18688773467,' amount ':' 1000 '},' method ':' post ',' expected ':' successful recharge '}] 
' '' 

# load test set 
suite unittest.TestSuite = () 
for Item in TEST_DATA: 
    suite.addTest (TestHttpApi (Item [ 'URL'],  
                              Item [ 'param'],
                              Item [ 'Method'], 
                              Item [ 'expected'],
                              'test_api')) 

# execute the test, output test report 
with open ( 'TestApi.html', ' wb +') as file:
    runner = HTMLTestRunner.HTMLTestRunner(file)
    runner.run(suite)
Copy the code
 

 

 

 

 

testing report:

 

 

Copy the code

Guess you like

Origin www.cnblogs.com/yangguanghandsome/p/12667512.html