其他测试框架 关键字驱动框架

测试开发体系 · 2023年12月05日 · 最后由 樱花小镇 回复于 2023年12月05日 · 4852 次阅读

关键字驱动框架

tep 简介

tep是一款帮你轻松编写 pytest 的测试框架。Try Easy Pytest!

快速入门

安装

pip install tep

验证安装成功:

tep -V
Current Version: V2.2.0

 ____o__ __o____   o__ __o__/_   o__ __o
  /   \   /   \   <|    v       <|     v\
       \o/        < >           / \     <\
        |          |            \o/     o/
       < >         o__/_         |__  _<|/
        |          |             |
        o         <o>           <o>
       <|          |             |
       / \        / \  _\o__/_  / \

新建项目

tep new demo
Created folder: demo
Created folder: demo/case
Created folder: demo/data
Created folder: demo/data/har
Created folder: demo/report
Created file:   demo/replay.py
Created file:   demo/run.py
Created file:   demo/conftest.py
Created file:   demo/pytest.ini
Created file:   demo/.gitignore
Created file:   demo/case/__init__.py
Created file:   demo/case/test_demo.py
Created file:   demo/data/UserDefinedVariables.yaml

编写用例

先看个简单示例,在case/test_demo.py编写用例:

def test(HTTPRequestKeyword):
    response = HTTPRequestKeyword("get", url="http://httpbin.org/status/200")
    assert response.status_code == 200

执行run.py后出现以下日志:

URL: http://httpbin.org/status/200
Method: GET
Headers: {"User-Agent": "python-requests/2.31.0", "Accept-Encoding": "gzip, deflate", "Accept": "*/*", "Connection": "keep-alive"}
Request Body: None
Status Code: 200
Response Body: 
Elapsed: 0.61046s

恭喜您,上手成功!

抓包自动生成用例

更简单点,抓包,自动生成用例。

编辑relay.py,指定 HAR 目录和用例目录:

import os

from tep.libraries.Config import Config
from tep.libraries.Har import Har

if __name__ == '__main__':
    profile = {
        "harDir": os.path.join(Config.BASE_DIR, "data", "har"),
        "desDir": os.path.join(Config.BASE_DIR, "case", "replay")
    }
    Har(profile).har2case()

默认 HAR 包放在data/har,生成用例放在case/replay

通过 Proxyman、Charles 等工具抓包后,导出 HAR 包到data/har目录中。如果还没来得及抓,可以下载现成的:

https://github.com/dongfanger/tep/blob/master/tests/demo/case/replay_demo.har

执行relay.py后就会自动生成replay_demo_test.py用例:

def test(HTTPRequestKeyword, JSONKeyword, VarKeyword):
    var = VarKeyword({})

    url = "http://httpbin.org/status/200"
    headers = JSONKeyword(r"""
{
    "Host":"httpbin.org",
    "Connection":"keep-alive",
    "accept":"text/plain",
    "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
    "Referer":"http://httpbin.org/",
    "Accept-Encoding":"gzip, deflate",
    "Accept-Language":"zh-CN,zh;q=0.9"
}
""")
    response = HTTPRequestKeyword("get", url=url, headers=headers)
    # user_defined_var = response.jsonpath("$.jsonpath")
    assert response.status_code < 400

让复杂的也变得简单,从登陆到下单示例:

def test(HTTPRequestKeyword, JSONKeyword, VarKeyword, login):
    headers = login()
    var = VarKeyword({
        "domain": "http://127.0.0.1:5000",
        "headers": headers
    })

    url = var["domain"] + "/searchSku" + "?skuName=book"
    response = HTTPRequestKeyword("get", url=url, headers=var["headers"])
    assert response.status_code < 400
    var["skuId"] = response.jsonpath("$.skuId")
    var["skuPrice"] = response.jsonpath("$.price")

    url = var["domain"] + "/addCart"
    body = JSONKeyword(r"""
{
    "skuId":"${skuId}",
    "skuNum":2
}
""")
    response = HTTPRequestKeyword("post", url=url, headers=var["headers"], json=body)
    assert response.status_code < 400
    var["skuNum"] = response.jsonpath("$.skuNum")
    var["totalPrice"] = response.jsonpath("$.totalPrice")

    url = var["domain"] + "/order"
    body = JSONKeyword(r"""
{
    "skuId":"${skuId}",
    "price":${skuPrice},
    "skuNum":${skuNum},
    "totalPrice":${totalPrice}
}
""")
    response = HTTPRequestKeyword("post", url=url, headers=var["headers"], json=body)
    assert response.status_code < 400
    var["orderId"] = response.jsonpath("$.orderId")

    url = var["domain"] + "/pay"
    body = JSONKeyword(r"""
{
    "orderId":"${orderId}",
    "payAmount":"0.2"
}
""")
    response = HTTPRequestKeyword("post", url=url, headers=var["headers"], json=body)
    assert response.status_code < 400
    assert response.jsonpath("$.success") == "true"

段落式组织代码,以关键字为驱动,pytest 也可以写的很轻松。

目录结构

  • case 存放用例文件
  • data 存放数据文件
  • report 存放报告文件
  • replay.py 抓包自动生成用例
  • run.py 执行用例入口

用例管理

  1. 用例所在目录称之为用例集。
  2. 用例全部写在一个文件里面,从上往下分成多个段落,每个段落视为一个测试步骤。
  3. 测试步骤分为①前置②接口请求③后置三大部分。

前置示例:

headers = login()
var = VarKeyword({
    "domain": "http://127.0.0.1:5000",
    "headers": headers
})

接口请求示例:

url = var["domain"] + "/addCart"
body = JSONKeyword(r"""
{
    "skuId":"${skuId}",
    "skuNum":2
}
""")
response = HTTPRequestKeyword("post", url=url, headers=var["headers"], json=body)
assert response.status_code < 400

后置示例:

var["skuNum"] = response.jsonpath("$.skuNum")
var["totalPrice"] = response.jsonpath("$.totalPrice")

编写用例

手动编写

Python 是解释性语言,py 文件和 yaml 文件一样,顺序执行。简单写,简单执行。

①新建test_开头或_test结尾的 py 文件。

②定义函数:

def test():

③在函数参数中输入关键字:

def test(HTTPRequestKeyword):

PyCharm 输入大写的K会出现语法提示,方便快速键入:

④编写请求:

def test(HTTPRequestKeyword):
    response = HTTPRequestKeyword("get", url="http://httpbin.org/status/200")

⑤添加断言:

def test(HTTPRequestKeyword):
    response = HTTPRequestKeyword("get", url="http://httpbin.org/status/200")
    assert response.status_code == 200

headers 和 body,使用多行字符串表示,通过JSONKeyword转为 dict:

url = "https://postman-echo.com/post"
headers = JSONKeyword(r"""
{
    "Host":"postman-echo.com",
    "User-Agent":"Go-http-client/1.1",
    "Content-Length":"28",
    "Content-Type":"application/json; charset=UTF-8",
    "Cookie":"sails.sid=s%3Az_LpglkKxTvJ_eHVUH6V67drKp0AGWW-.PidabaXOnatLRP47hVyqqepl6BdrpEQzRlJQXtbIiwk",
    "Accept-Encoding":"gzip",
    "sails.sid":"s%3Az_LpglkKxTvJ_eHVUH6V67drKp0AGWW-.PidabaXOnatLRP47hVyqqepl6BdrpEQzRlJQXtbIiwk"
}
""")
body = JSONKeyword(r"""
{
    "foo1":"HDnY8",
    "foo2":12.3
}
""")
response = HTTPRequestKeyword("post", url=url, headers=headers, json=body)

HAR 包转换

单个文件

import os

from tep.libraries.Config import Config
from tep.libraries.Har import Har


def test():
    profile = {
        "harFile": os.path.join(Config.BASE_DIR, "case", "replay_demo.har")
    }
    Har(profile).har2case()

指定目录

import os

from tep.libraries.Config import Config
from tep.libraries.Har import Har

if __name__ == '__main__':
    profile = {
        "harDir": os.path.join(Config.BASE_DIR, "data", "har"),
        "desDir": os.path.join(Config.BASE_DIR, "case", "replay")
    }
    Har(profile).har2case()

如果 HAR 包已经生成了用例,第二次执行会跳过。通过配置overwrite: True可重新覆盖:

import os

from tep.libraries.Config import Config
from tep.libraries.Har import Har

if __name__ == '__main__':
    profile = {
        "harDir": os.path.join(Config.BASE_DIR, "data", "har"),
        "desDir": os.path.join(Config.BASE_DIR, "case", "replay"),
        "overwrite": True
    }
    Har(profile).har2case()

回放对比

profile 配置"replay": True,开启回放对比:

import os

from loguru import logger

from tep.libraries.Config import Config
from tep.libraries.Har import Har


def test():
    har_file = os.path.join(Config.BASE_DIR, "case", "har", "demo.har")
    profile = {"replay": True}
    Har(har_file, profile).har2case()

等待 HAR 包转换为 pytest 用例后,执行 pytest 用例,即会对比:

变量管理

全局变量data/UserDefinedVariables.yaml中定义,通过UserDefinedVariablesKeyword关键字读取。

局部变量test()函数内定义,通过VarKeyword关键字设值和取值。

其他维度变量可在data目录下新建 YAML/JSON 文件,通过DataKeyword读取。

接口关联

初始化

test()函数中通过VarKeyword关键字初始化一个变量池:

var = VarKeyword({})

取值

在步骤后置中通过 jsonpath 取值:

var["skuId"] = response.jsonpath("$.skuId")

存入变量池中。

传值

通过JSONKeyword关键字,在字符串中使用${}标记变量:

body = JSONKeyword(r"""
{
    "skuId":"${skuId}",
    "skuNum":2
}
""")

框架会使用变量池中同名变量进行替换。

数据管理

PyCharm 就是 “测试平台”,单个用例文件就是平台界面,数据和代码都写在里面。

若要分离,可从文件读取。

接口管理

以空间换时间,接口不单独管理,跟随用例复制多份,用例维护自己的接口参数。

若要复用,自定义关键字。

原生断言

def test_assert_equal():
    assert 1 == 1


def test_assert_not_equal():
    assert 1 != 2


def test_assert_greater_than():
    assert 2 > 1


def test_assert_less_than():
    assert 1 < 2


def test_assert_less_or_equals():
    assert 2 >= 1
    assert 2 >= 2


def test_assert_greater_or_equals():
    assert 1 <= 2
    assert 1 <= 1


def test_assert_length_equal():
    assert len("abc") == len("123")


def test_assert_length_greater_than():
    assert len("hello") > len("123")


def test_assert_length_less_than():
    assert len("hi") < len("123")


def test_assert_length_greater_or_equals():
    assert len("hello") >= len("123")
    assert len("123") >= len("123")


def test_assert_length_less_or_equals():
    assert len("123") <= len("hello")
    assert len("123") <= len("123")


def test_assert_string_equals():
    assert "dongfanger" == "dongfanger"


def test_assert_startswith():
    assert "dongfanger".startswith("don")


def test_assert_regex_match():
    import re
    assert re.findall(r"don.*er", "dongfanger")


def test_assert_contains():
    assert "fang" in "dongfanger"
    assert 2 in [2, 3]
    assert "x" in {"x": "y"}.keys()


def test_assert_type_match():
    assert isinstance(1, int)
    assert isinstance(0.2, float)
    assert isinstance(True, bool)
    assert isinstance(3e+26j, complex)
    assert isinstance("hi", str)
    assert isinstance([1, 2], list)
    assert isinstance((1, 2), tuple)
    assert isinstance({"a", "b", "c"}, set)
    assert isinstance({"x": 1}, dict)

测试报告

支持 pytst-html 报告和 Allure 报告。

默认为 pytest-html,无需单独安装。

Allure 需要安装 Java,下载 Allure 安装包,解压后将 bin 目录添加到系统环境变量 Path:

https://github.com/allure-framework/allure2/releases

协议支持

默认协议为 HTTP1,添加http2=True参数后,使用 HTTP2 协议:

def test(HTTPRequestKeyword):
    ro = HTTPRequestKeyword("get", url="http://httpbin.org/status/200", http2=True)
    assert ro.response.status_code == 200

HAR 包转换用例时,在 profile 配置"http2": True也能使用 HTTP2 协议。

接口重试

给用例添加@retry,自定义重试策略,比如重试 3 次,间隔 2 秒。

根据 CODE 码进行重试:

from loguru import logger
from tenacity import retry, stop_after_attempt, wait_fixed


@retry(
    stop=stop_after_attempt(3),
    wait=wait_fixed(2),
    before=lambda _: logger.info("Retrying..."),
    after=lambda _: logger.info("Retry completed.")
)
def test(HTTPRequestKeyword):
    ro = HTTPRequestKeyword("get", url="http://127.0.0.1:5000/retry/code")
    ro.response.raise_for_status()  # Add this statement

根据 Exception 类型进行重试:

from loguru import logger
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type


@retry(
    stop=stop_after_attempt(3),
    wait=wait_fixed(2),
    retry=retry_if_exception_type(TypeError),
    before=lambda _: logger.info("Retrying..."),
    after=lambda _: logger.info("Retry completed.")
)
def test_exception(HTTPRequestKeyword):
    ro = HTTPRequestKeyword("get", url="http://127.0.0.1:5000/retry/200")
    raise TypeError

关键字

关键字的本质是 pytest fixture 函数。

内置关键字

命名方式:大驼峰命名,Keyword结尾

HTTPRequestKeyword

与 requests.request 用法完全一致

JSONKeyword

将字符串转为 dict,支持${}替换变量

UserDefinedVariablesKeyword

读取data/UserDefinedVariables.yaml作为全局变量

DataKeyword

读取 data 目录下 YAML/JSON 文件作为自定义变量

自定义关键字

命名方式:下划线命名。

自定义关键字需新建 fixture 包,文件名以fixture_开头:

login

登录接口,可定义为全局只登陆一次

mysql_execute

连接 MySQL 数据库,执行 SQL 语句

关键字使用示例请查看https://github.com/dongfanger/tep/tree/master/tests/demo/case

源码地址

https://github.com/dongfanger/tep

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册