python+requests做接口测试

使用python+request是就可以做接口测试,脚本编写比较简单,只要掌握大致的逻辑新手也可以做简单的接口测试。

当然了,postman和jmeter也是不错的接口测试工具,这看个人的喜好了

import requests
import unittest
import time

import payload as p

class Agile(unittest.TestCase):
    def setUp(self):
        self.url = ""
    def the_chart(self):
        '''图表数据'''
        try:
            response = requests.request("POST", url=self.url+"",
                                        data=p.payload_01, headers=p.headers)
            self.result = response.json()
            self.assertEqual(self.result['data'][0], ['上海市', '40015'])
            print(self.result['data'][0])
        except:
            print('图表断言结果出错')

    def the_data_set(self):
        '''图表设计数据'''
        try:
            response = requests.request("POST", url=self.url + "",
                                        data=p.payload_02, headers=p.headers)
            self.result = response.json()
            self.assertEqual(self.result[0], '三亚市')
            print(self.result[0])
        except:
            print('数据断言错误')

    def getWidgetLis(self):
        try:
            response = requests.request("GEt",url=self.url + "",headers=p.headers)
            self.result = response.json()
            print(self.result[9]['id'],self.result[9]['name'])
        except:
            print('数据校验错误')


if __name__ == "__main__":
    # 构造测试集
    suite = unittest.TestSuite()
    suite.addTest(Agile("_cthehart"))
    time.sleep(3)
    suite.addTest(Agile("the_data_set"))
    time.sleep(3)
    suite.addTest(Agile("getWidgetLis"))
    # 执行测试
    runner = unittest.TextTestRunner()
    runner.run(suite)

 

转载于:https://www.cnblogs.com/mtfan01/p/9298722.html

你可能感兴趣的:(python+requests做接口测试)