Exemple #1
0
#-*- coding: utf-8 -*-

import requests
from API_8.common.config import config
from API_8.common import logger

logger = logger.get_logger(__name__)


class HTTPRequest:
    '''
    使用这类的request方法去完成不同的HTTP请求,并且返回响应结果
    '''
    def request(self, method, url, data=None, json=None, cookies=None):

        method = method.upper()  #强制转成全大写

        if type(data) == str:
            data = eval(data)  # str 转成字典

        #拼接请求的url
        url = config.get('api', 'pre_url') + url

        if method == 'GET':
            resp = requests.get(url, params=data,
                                cookies=cookies)  # resp 是Response对象
        elif method == 'POST':
            if json:
                resp = requests.post(url, json=json, cookies=cookies)
            else:
                resp = requests.post(url, data=data, cookies=cookies)
# -*- coding: utf-8 -*-
# @Time    : 2020/6/19 11:06
# @Author  : zc
# @Email   : [email protected]
# @File    : test_login.py
# @Software: PyCharm

import unittest
from API_8.common.http_request import HttpRequest, HttpRequestSession
from API_8.common import do_excel
from API_8.common import contants
from ddt import ddt, data
from API_8.common import logger

logger = logger.get_logger(__name__)  #__name__  这个文件名


@ddt
class LoginTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'login')
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequestSession()

    @data(*cases)
    def test_login(self, case):
        logger.info('开始测试:{}'.format(case.title))
        resp = self.http_request.request(case.method, case.url, data=case.data)