Ejemplo n.º 1
0
class TestHttpRequest(unittest.TestCase):  #!!!这里要继承TestCase
    def setUp(self):
        #创建操作excel的实例
        self.t = DoExcel('test_case.xlsx', 'test_data')
        print("开始测试")

    @data(*test_data)
    def test_http_request(self, a):
        print("测试数据是:", a)
        print("目前正在执行第%s条用例" % a[0])
        global COOKIES
        res = HttpRequest(a[4], eval(a[5])).httpRequest(
            a[3], cookies=COOKIES)  #!!!要将从excel中读出的字典格式装换一下
        if res.cookies != {}:  #判断cookies是否为空用{},或用len(res.cookies)==0
            COOKIES = res.cookies
        print(res.json())
        try:
            self.assertEqual(str(a[6]), res.json()['code'])  #!!!预期结果要转换成str
            result = 'PASS'
        except AssertionError as e:
            result = 'FAIL'
            raise e  #!!!终止后面的代码
        finally:
            self.t.write_data(a[0] + 1, str(res.json()), result)

    def tearDown(self):
        print("测试结束")
Ejemplo n.º 2
0
        """
        添加公共的请求头
        :param one_dict: 请求头参数,字典类型
        :return:
        """
        self.session.headers.update(one_dict)

    def close(self):
        """

        :return:
        """
        self.session.close()


if __name__ == '__main__':
    excel_filename = "create_user.xlsx"
    sheet_name = "create_user"
    doexcel = DoExcel(excel_filename, sheet_name)
    data = doexcel.read_data()
    request_header = {
        "Authorization":
        "Bearer GIGGXI0XSkW3aKxys8xPYxPNP6xAG4mT8sLwJYJvRCOrDD8dZdlcE1wOTmPCglC4n0HxwX9cQ8s3Jbas-rFFOK9ybQdmBc7xvaCGupVfwB3VKaCQDKn1o8hzktg4Im1hw7xWTih",
        "Accept": "application/json"
    }
    create_user = Request()
    create_user.add_header(request_header)
    res = create_user.send(data[0].method, data[0].url, json=data[0].data)
    print(res.json())
    doexcel.write_data(data[0], res.json()["message"], "成功")