def __init__(self):
     # 连接数据库,创建游标。
     # 1、建立连接
     self.conn = pymysql.connect(host=red_conf.get("mysql", "host"),
                                 port=red_conf.getint("mysql", "port"),
                                 user=red_conf.get("mysql", "user"),
                                 password=red_conf.get("mysql", "password"),
                                 database=red_conf.get("mysql", "database"),
                                 charset="utf8",
                                 cursorclass=pymysql.cursors.DictCursor)
     # 2、创建游标
     self.cur = self.conn.cursor()
Ejemplo n.º 2
0
 def setUpClass(cls) -> None:
     myLogger.info("==================确认用户注册模块接口测试开始==================")
     register_username = red_conf.get("register_user", "username")
     register_email = red_conf.get("register_user", "email")
     register_pwd = red_conf.get("register_user", "password")
     request_register_data = {
         "username": register_username,
         "email": register_email,
         "password": register_pwd,
         "password_confirm": register_pwd
     }
     register_url = "/user/register/"
     set_request(register_url, "POST", request_register_data)
Ejemplo n.º 3
0
    def test_is_email_register(self, cases):
        self.__dict__['_testMethodDoc'] = str(
            cases["case_id"]) + "-" + cases["case_tittle"]
        myLogger.info("==================用例{}执行开始==================".format(
            str(cases["case_id"])))
        if cases["url"].find(REPLACE_NOREGISTER_EMAIL_MARK) != -1:
            email = get_new_email()
            cases = replace_mark_with_data(cases,
                                           REPLACE_NOREGISTER_EMAIL_MARK,
                                           email)
        elif cases["url"].find(REPLACE_REGISTER_EMAIL_MARK) != -1:
            email = red_conf.get("register_user", "email")
            cases = replace_mark_with_data(cases, REPLACE_REGISTER_EMAIL_MARK,
                                           email)
        else:
            pass
        response_is_email_register = set_request(cases["url"], cases["method"])
        myLogger.info("实际结果为:{}".format(response_is_email_register.json()))

        expected = json.loads(cases["expect_res"])
        myLogger.info("期望结果为:{}".format(expected))

        # 断言
        try:
            self.assertEqual(response_is_email_register.status_code,
                             cases['status_code'])
            self.assertEqual(response_is_email_register.json()["email"],
                             expected["email"])
            self.assertEqual(response_is_email_register.json()["count"],
                             expected["count"])
        except AssertionError:
            myLogger.info("用例执行失败")
            raise
        myLogger.info("==================用例结束==================")
Ejemplo n.º 4
0
    def test_login(self,cases):
        self.__dict__['_testMethodDoc'] = str(cases["case_id"]) + "-" + cases["case_tittle"]
        myLogger.info("==================用例{}执行开始==================".format(str(cases["case_id"])))
        register_username = red_conf.get("register_user", "username")
        register_password = red_conf.get("register_user", "password")
        if cases["request_data"].find(REPLACE_REGISTER_USERNAME_MARK) != -1 or cases["request_data"].find(
                REPLACE_REGISTER_PASSWORD_MARK) != -1 or cases["request_data"].find(REPLACE_NO_REGISTER_USERNAME_MARK) \
                != -1 or cases["request_data"].find(REPLACE_NO_REGISTER_PASSWORD_MARK) != -1:
            no_register_username = get_new_username(int(cases['random_username_len']))
            no_register_password = get_password(int(cases['random_password_len']))
            cases = replace_mark_with_data(cases, REPLACE_REGISTER_USERNAME_MARK, register_username)
            cases = replace_mark_with_data(cases, REPLACE_REGISTER_PASSWORD_MARK, register_password)
            cases = replace_mark_with_data(cases, REPLACE_NO_REGISTER_USERNAME_MARK, no_register_username)
            cases = replace_mark_with_data(cases, REPLACE_NO_REGISTER_PASSWORD_MARK, no_register_password)
        if cases['check_sql']:
            user_id = db.select_one_data(cases['check_sql'])["id"]
            cases = replace_mark_with_data(cases, REPLACE_USER_ID_MARK, str(user_id))
            myLogger.info("替换后的最终测试数据:{}".format(cases))

        expected = json.loads(cases['expect_res'])
        myLogger.info("预期结果:{}".format(expected))

        request_data = json.loads(cases['request_data'])
        response_login = set_request(cases['url'], cases["method"], request_data)
        myLogger.info("实际结果:{}".format(response_login.json()))

        # 断言
        try:
            self.assertEqual(response_login.status_code, cases['status_code'])
            if cases['check_sql']:
                self.assertIsNotNone(response_login.json()["token"])
                self.assertEqual(response_login.json()["user_id"], expected["user_id"])
                self.assertEqual(response_login.json()["username"], expected["username"])
                try:
                    if expected["non_field_errors"]:
                        self.assertEqual(response_login.json()["non_field_errors"], expected["non_field_errors"])
                    elif expected["username"]:
                        self.assertEqual(response_login.json()["username"], expected["username"])
                    elif expected["passwprd"]:
                        self.assertEqual(response_login.json()["password"], expected["password"])
                except KeyError:
                    pass

        except AssertionError:
            myLogger.info("用例执行失败")
            raise
Ejemplo n.º 5
0
def __pre_url(url):
    '''
    拼接接口的url地址。
    :param url:读取的excel里面的url
    :return:
    '''
    base_url = red_conf.get("server", "base_url")
    if url.startswith("/"):
        return base_url + url
    else:
        return base_url + "/" + url
def get_old_user():
    '''
    从配置文件获取指定的用户名和密码
    确保此帐号,在系统当中是注册了的。
    返回:用户名和密码。
    :return:
    '''
    from Common.handle_conf import red_conf
    from Common.handle_requests import set_request
    user = red_conf.get("register_user", "username")
    password = red_conf.get("register_user", "password")
    email = red_conf.get("register_user", "email")
    # 如果数据库查找到user,就直接返回。如果没有,则调用注册接口注册一个。
    # 不管注册与否,直接调用注册接口。
    set_request(
        "/user/register/", "post", {
            "username": user,
            "email": email,
            "password": password,
            "password_confirm": password
        })
    return user, password
Ejemplo n.º 7
0
def replace_by_regular(data):
    '''
    将字符串当中,匹配#(.*?)#部分,替换对应的真实数据。
    将真实数据只从2个地方去获取:1.配置文件中的data区域;2.EvnData的类属性(全应是字符串类型)
    :param data: 字符串
    :return: 返回替换后的字符串
    '''
    res = re.findall("#(.*?)#", data)
    # print(res)
    # 标识符对应的值,来自于: 1.环境变量 2.配置文件
    if res:
        for item in res:
            # noinspection PyBroadException
            try:
                value = red_conf.get("data", item)
            except Exception as e:
                try:
                    value = getattr(EnvData, item)
                except AttributeError:
                    continue
            data = data.replace("#{}#".format(item), value)
    return data
    def test_projects(self, cases):
        self.__dict__['_testMethodDoc'] = str(
            cases["case_id"]) + "-" + cases["case_tittle"]
        myLogger.info("==================用例{}执行开始==================".format(
            str(cases["case_id"])))
        projects_rename = red_conf.get("project", "name")

        # 以请求数据中的数据标记为源头,初步替换真实数据
        if cases['request_data'].find(REPLACE_PROJECTS_NAME_MARK
                                      ) != -1 or cases['request_data'].find(
                                          REPLACE_PROJECTS_LEADER_MARK
                                      ) != -1 or cases['request_data'].find(
                                          REPLACE_PROJECTS_TESTER_MARK
                                      ) != -1 or cases['request_data'].find(
                                          REPLACE_PROJECTS_PROGRAMMER_MARK
                                      ) != -1 or cases['request_data'].find(
                                          REPLACE_PROJECTS_PUBLISH_APP_MARK
                                      ) != -1 or cases['request_data'].find(
                                          REPLACE_PROJECTS_DESC_MARK) != -1:
            name = get_new_projects_datas(int(cases['random_name_len']))
            leader = get_new_projects_datas(int(cases['random_leader_len']))
            tester = get_new_projects_datas(int(cases['random_tester_len']))
            programmer = get_new_projects_datas(
                int(cases['random_programmer_len']))
            publish_app = get_new_projects_datas(
                int(cases['random_publish_app_len']))
            desc = get_new_projects_datas(int(cases['random_desc_len']))
            cases = replace_mark_with_data(cases, REPLACE_PROJECTS_NAME_MARK,
                                           name)
            cases = replace_mark_with_data(cases, REPLACE_PROJECTS_LEADER_MARK,
                                           leader)
            cases = replace_mark_with_data(cases, REPLACE_PROJECTS_TESTER_MARK,
                                           tester)
            cases = replace_mark_with_data(cases,
                                           REPLACE_PROJECTS_PROGRAMMER_MARK,
                                           programmer)
            cases = replace_mark_with_data(cases,
                                           REPLACE_PROJECTS_PUBLISH_APP_MARK,
                                           publish_app)
            cases = replace_mark_with_data(cases, REPLACE_PROJECTS_DESC_MARK,
                                           desc)
            if cases['request_data'].find(REPLACE_PROJECTS_RENAME_MARK) != -1:
                cases = replace_mark_with_data(cases,
                                               REPLACE_PROJECTS_RENAME_MARK,
                                               projects_rename)

        # 发送请求
        request_data = json.loads(cases['request_data'])
        response_projects = set_request(cases['url'],
                                        cases['method'],
                                        request_data,
                                        token=EnvData.token)

        # 继续替换期望结果中的数据为真实数据
        if cases['check_sql']:
            project_id = db.select_one_data(cases['check_sql'])['id']
            project_create_time = db.select_one_data(
                cases['check_sql'])['create_time']
            cases = replace_mark_with_data(cases, REPLACE_PROJECTS_DB_ID_MARK,
                                           str(project_id))
            cases = replace_mark_with_data(
                cases, REPLACE_PROJECTS_DB_GREATE_TIME_MARK,
                str(project_create_time))
            myLogger.info("数据替换完毕")

        # 处理期望结果
        expected = json.loads(cases['expect_res'])
        try:
            if expected['create_time']:
                expected['create_time'] = handle_time(expected['create_time'])
        except KeyError:
            pass
        myLogger.info("期望结果为:{}".format(expected))
        myLogger.info("实际结果为:{}".format(response_projects.json()))

        # 断言
        try:
            self.assertEqual(response_projects.status_code,
                             cases['status_code'])
            self.assertEqual(response_projects.json(), expected)

        except AssertionError:
            myLogger.info("用例执行失败")
            raise