本文共 1974 字,大约阅读时间需要 6 分钟。
pytest 是一个基于 unittest 的现代化单元测试框架,具有自动发现测试模块和方法的能力。其独特之处在于:
assert + 表达式即可完成验证conftest.py 实现pip install pytestpip install pytest-htmltest_*.py 或 *_test.pytest_ 开头Test 开头且不包含 __init__ 方法@pytest.mark.标签名 或在类/模块中定义@pytest.mark.标签名pytestmark = pytest.mark.标签名pytestmark = [pytest.mark.标签1, pytest.mark.标签2] # 多标签模式
@pytest.mark.slowdef test_login_success(self, init_driver): init_driver[1]....
conftest.py 文件中定义def pytest_configure(config): config.addinivalue_line("markers", "smoke1:标记只运行冒烟用例") config.addinivalue_line("markers", "demo1:示例运行")@pytest.mark.usefixtures("fixture_name") 装饰pytest.fixture 装饰器定义 fixture@pytest.mark.parametrize 定义参数化规则@pytest.mark.parametrize("参数名", [(1, 2, 3), (4, 5, 6)])def test_add(self, a, b, c): assert a + b == c@pytest.mark.parametrize("x", [1, 2])@pytest.mark.parametrize("y", [2, 3])def test_foo(x, y): pass1,2、1,3、2,2、2,3pytest-rerunfailures:pip install pytest-rerunfailurespytest --reruns 2 # 失败用例重试 2 次pytest --reruns-delay 5 # 失败用例间隔 5 秒重试
pytest-html 插件,使用命令生成报告pytest --html=report/test.html
--junitxml 生成 XML 报告pytest --junitxml=report/test.xml
allure-pytest 插件,使用命令生成 Allure 报告pytest --alluredir=report/allure
D:\allure-2.13.5slave-agent.jnlp 直接运行或作为系统服务安装通过以上配置,pytest 可以帮助开发者高效、可靠地执行单元测试任务,并生成多种样式的测试报告。
转载地址:http://jnafk.baihongyu.com/