httprunner2.x--HttpRunner的hook(钩子)机制:(setup/teardown)对数据清理

正常情况下的注册接口,使用用户名注册成功后,就不能使用相同的用户名进行注册

那如果我们想使用一个用户名进行重复注册和查看注册后是否在数据库中存在怎么办呢

使用setup_hooks前置将数据库中的数据清理掉

使用teardown_hooks后置查询数据库中的数据

所以我们需要连接数据库

debugtalk.py创建数据库连接

import pymysql

dbinfo = {
    "host": "127.0.0.1",
    "user": "user",
    "password": "password",
    "port": 3306
}


class DbConnect():
    def __init__(self, db_cof=dbinfo, database=""):
        self.db_cof = db_cof
        # 打开数据库连接
        self.db = pymysql.connect(database=database,
                                  cursorclass=pymysql.cursors.DictCursor,
                                  **db_cof)
        # 使用cursor()方式获取操作游标
        self.cursor = self.db.cursor()

    def select(self, sql):
        # SQL 查询语句
        # sql = "SELECT * FROM EMPLOYEE \
        #       WHERE INCOME > %s" % (1000)
        self.cursor.execute(sql)
        results = self.cursor.fetchall()
        return results

    def execute(self, sql):
        # SQL 删除、提交、修改语句
        # sql = "DELETE FROM EMPLOYEE WHERE AGE > %s" % (20)
        try:
            # 执行SQL语句
            self.cursor.execute(sql)
            # 提交修改
            self.db.commit()
        except:
            # 发生错误时回滚
            self.db.rollback()

    def close(self):
        # 关闭连接
        self.db.close()


def select_sql(sql):
    '''查询函数'''
    db = DbConnect(database="database")
    result = db.select(sql)
    db.close()
    return result


def execute_sql(sql):
    '''执行函数(新增、删除、修改)'''
    db = DbConnect(database="database")
    db.execute(sql)
    db.close()

 

创建setup_hooks函数

def del_date(username):
    """
    hooks函数,查询用户如果存在就删除
    :param username:
    :return:
    """
    sel_sql = """select id,username,password,email,`status`,CAST(create_time AS CHAR) AS create_time,CAST(update_time AS CHAR) AS update_time from UserInfo where username='%s';""" %username
    del_sql = """DELETE from UserInfo WHERE username='%s';""" %username
    if select_sql(sel_sql):
        execute_sql(del_sql)
        print('%s已经清理'%username)
    else:
        print('%s未注册,请先注册' %username)

创建teardown_hooks函数

def sel_date(username):
    sel_sql = """select id,username,password,email,`status`,CAST(create_time AS CHAR) AS create_time,CAST(update_time AS CHAR) AS update_time from UserInfo where username='%s';""" % username
    if select_sql(sel_sql):
        print('%s注册成功'%username)

  

regirster.yml

name: httprunnermanager平台注册接口
variables:
    p_account: test1112
base_url: http://localhost:8000
request:
    url: /api/register/
    method: POST
    headers:
        Content-Type: "application/json"
        User-Agent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
          (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
        X-Requested-With: XMLHttpRequest
    json:
        account: $p_account
        email: "test11112@123.com"
        password: '123123'
        repassword: '123123'
validate:
    - eq: ["status_code", 200]

  

测试用例层调用hooks

config:
    name: 注册用户测试用例
    variables:
        p_account: test1112
    base_url: http://localhost:8000

    setup_hooks:
        - ${del_date($p_account)}
    teardown_hooks:
        - ${sel_date($p_account)}

teststeps:
-
    name: 调用注册接口
    api: api/register.yml
    variables:
        p_account: test1112

    validate:
        - eq: ["status_code", 200]

  

执行结果

 

 

测试步骤层调用hooks

config:
    name: 注册用户测试用例
    variables:
        p_account: test1112
    base_url: http://localhost:8000

teststeps:
-
    name: 调用注册接口
    # 测试步骤层调用hooks函数
    setup_hooks:
        - ${del_date($p_account)}
    teardown_hooks:
        - ${sel_date($p_account)}
        -
    api: api/register.yml
    variables:
        p_account: test1112
    validate:
        - eq: ["status_code", 200]
        - eq: [content, 恭喜您,账号已成功注册]

  

执行结果

 

posted @ 2021-06-04 11:23  莫使娇躯空对月  阅读(267)  评论(0编辑  收藏  举报