• python +requests做接口自动化


    1.对方法进行封装
    #1、封装get、post
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    import json
    
    import requests
    
    
    class RunMethod:
    
        def post_main(self, url, data, header=None, params=None):
            res = None
            if header is not None:
                res = requests.post(url=url, data=data, headers=header, params=params, verify=False)
            else:
                res = requests.post(url=url, data=data, verify=False)
            return res
    
    
        def get_main(self, url, data=None, header=None):
            res = None
            if header is not None:
                res = requests.get(url=url, params=data, headers=header, verify=False)
            else:
                res = requests.get(url=url, params=data, verify=False)
            return res
    
        def run_main(self, method, url, data=None, header=None, params=None):
            res = None
            r = None
            if method == 'Post':
                res = self.post_main(url, data, header, params)
            else:
                res = self.get_main(url, data, header)
            # return json.dumps(res, ensure_ascii=False)
            # print(res.url)
            # print(res.text)
            #r = res.text
            #d = json.loads(r)
            # r = res.content
            # par = json.loads(r)
            return res.json()
    
    

    2.新建测试case,调用封装好的接口

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    ' a test module '
    import urllib3
    import unittest
    import time
    import os
    import sys
    from request.test_2 import RunMethod
    from HTMLTestRunner import HTMLTestRunner


    class Test(unittest.TestCase):
    def test_case01(self):
    print(1111)
    urllib3.disable_warnings()
    self.run = RunMethod()
    url = 'http://httpbin.org/get'

    querystring = {}
    data = {}
    params = {'key1': 'value1', 'key2': 'value2'}
    # data = "{"msg":"QzCNH3+l2/P4w3jTqacK+VGcG2bZf1D1/Tk9qkaGhzxKkPz7h8btjdjWR542vq7zE70ouc+asLft1OpoijOcJwP+GP7"
    # "/cnMTnRa1pe7bbwuc+66G1BDgnt4vHCPTk7wzxqmaWOaRFHi8SqheJo8zPVH2a13loqr7k9Xc83wEcRA="} "
    header = {}

    res = self.run.run_main("get", url, data, header, params)
    # self.accessToken = res['data']['accessToken']
    print(333)
    # self.userId = res['data']['userId']
    print(res)
    print(222)

    # def test_getInterviewRole_02(self):
    # url = "https://x-chat-test.zmlearn.com/gateway/zmc-personal-center/api/courseMsg/getUserMessages"
    # querystring = {"access_token": self.accessToken}
    # data = {
    # "userId": self.userId
    # }
    # header = {
    # "Content-Type": "application/x-www-form-urlencoded"
    # }
    # res = self.run.run_main("Post", url, data, header, querystring)
    # print(res)
    # # self.hall = res['data'][0]['id']
    def test_case02(self):
    urllib3.disable_warnings()
    self.run = RunMethod()
    url = 'http://httpbin.org/post'
    data = {"key1": "value1", "key2": "value2"}
    params = {"key1": "value1", "key2": "value2"}
    # data = "{"msg":"QzCNH3+l2/P4w3jTqacK+VGcG2bZf1D1/Tk9qkaGhzxKkPz7h8btjdjWR542vq7zE70ouc+asLft1OpoijOcJwP+GP7"
    # "/cnMTnRa1pe7bbwuc+66G1BDgnt4vHCPTk7wzxqmaWOaRFHi8SqheJo8zPVH2a13loqr7k9Xc83wEcRA="} "
    header = {}

    res = self.run.run_main("Post", url, data, header, params)
    # self.accessToken = res['data']['accessToken']
    print(666)
    # self.userId = res['data']['userId']
    print(res)
    print(777)


    if __name__ == '__main__':

    A = unittest.TestSuite()
    # fp = open('./request/result.html', 'wb') # 定义测试报告存放路径
    # runner = HTMLTestRunner(stream=fp, title='百度搜索测试报告', description='用例执行情况') # 定义测试报告
    # runner.run(testunit) # 执行测试用例
    # fp.close()
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    filename = './report/' + now + '_result.html'
    fp = open(filename, 'wb')
    runner = HTMLTestRunner(stream=fp,
    title='Guest Manage System Interface Test Report',
    description='Implementation Example with: ')
    runner.run(A)
    fp.close()
  • 相关阅读:
    Linux下启动时间优化专题
    如何展开Linux Memory Management学习?
    《Systems Performance》阅读笔记及收获
    Linux时间子系统之四:Timer在用户和内核空间流程
    Win7 下安装ubuntu14.04双系统
    ubuntu 上使用valgrind
    Observer模式
    从C++到java
    gcc 0长数组学习
    Linux中10个有用的命令行补齐命令
  • 原文地址:https://www.cnblogs.com/cc233/p/14832554.html
Copyright © 2020-2023  润新知