pytest 笔记
  0voAMrgZhFuV 2023年11月02日 35 0

常用组件

pytest

本身

pytest-html

生成 html 报告

pytest-xdist

多线程执行

pytest-ordering

控制用例的执行顺序

pytest-rerunfailures

失败用例重跑

pytest-base-url

基础路径

allure-pytest

生成 allure 报告

requirements.txt

requests
loguru
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
pytest-base-url
allure-pytest

pytest 三种运行方式

命令行

直接在当前目录执行 pytest

主函数

# -*- coding: utf-8 -*-
import pytest

if __name__ == '__main__':
    pytest.main()

配置文件

根目录添加 pytest.ini, 文件名固定

[pytest]
# 命令行参数
# -v: 打印详细运行日志信息
# -s: 控制台输出结果
addopts = -vs

查找规则

默认查找规则

如果没有指定命令行参数, 则按照如下规则

  1. 在当前目录开始查找可用的测试用例
  2. 在该目录和所有子目录递归查找测试模块
  3. 测试模块指文件名为 test_*.py*_test.py 的文件
  4. 在测试模块中查找以 test_ 开头的函数名
  5. 查找名字以 Test 开头的类, 其中首先筛选包含 __init__ 函数的类,再查找以 test_ 开头类中的方法
  6. 所有的包都有有 __init__.py 文件

修改查找规则

# 用例查找规则
testpaths = ./testcases

# 模块查找规则
python_files=test_*.py
;python_files=atest_two.py

# 类查找规则
;python_classes = Test*

# 方法查找规则
;python_functions = test_*

标记

只执行标记的测试用例

[pytest]
# 命令行参数
# -v: 打印详细运行日志信息
# -s: 控制台输出结果
addopts = -vs -m "smoke"
# addopts = -vs -m "smoke or user" # smoke 和 user 标记会执行

# 用例查找规则
testpaths = ./testcases

markers=
    smoke: 冒烟测试


# -*- coding: utf-8 -*-
import pytest
from loguru import logger


@pytest.mark.smoke
def test_one():
    logger.info("test one")
    assert 1 == 1


def test_two():
    logger.info("test two")
    assert 1 == 1


def test_three():
    logger.info("test three")
    assert 1 == 1

前置后置固件

Pytest测试框架(二):pytest 的setup/teardown方法

fixture

Pytest测试框架(三):pytest fixture 用法

















【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  1BVmdlLr07sm   2023年11月30日   73   0   0 HTMLcss
0voAMrgZhFuV
作者其他文章 更多
最新推荐 更多