目录
前言:
使用Pytest来运行yaml文件来驱动Appium自动化测试是一种方便且灵活的方法。通过将测试数据和测试逻辑分离,您可以更轻松地管理和扩展测试用例。
本文主要介绍一下 pytest hook 方法是怎么运行 yaml 文件做测试的 (喜欢其他方式也可以用 xlsx,sql 等),怎么与 Appium 结合,来驱动 Appium 做自动化测试。
该运行方式参照 pytest --doctest 参数运行的方法, 想深入了解的同学可以查看 _pytest/doctest.py 源码
获取 yaml 文件
使用 pytest_collect_file 钩子函数,在脚本运行之前过滤 .yml 文件
def pytest_collect_file(parent, path):
# 获取文件.yml 文件
if path.ext == ".yml" and path.basename.startswith("test"):
return YamlFile(path, parent)
读取 yml 转成 json 移交至 YamlTest 类
class YamlFile(pytest.File):
# 读取文件内容
def collect(self):
import yaml
raw = yaml.safe_load(self.fspath.open(encoding='utf-8'))
for name, values in raw.items():
yield YamlTest(name, self, values)
YamlTest 测试类
下面就是测试类了,这里我们 Appium 还是使用单例初始化,方便所有测试用例继承一个 session
Appium 初始化
class Singleton(object):
"""单例
ElementActions 为自己封装操作类"""
Action = None
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
desired_caps={}
host = "http://localhost:4723/wd/hub"
driver = webdriver.Remote(host, desired_caps)
Action = ElementActions(driver, desired_caps)
orig = super(Singleton, cls)
cls._instance = orig.__new__(cls, *args, **kw)
cls._instance.Action = Action
return cls._instance
class DriverClient(Singleton):
pass
Pytest 测试类
测试类初始化,这里要说一下,测试类一定要继承 pytest.Item 方法
class YamlTest(pytest.Item):
def __init__(self, name, parent,