pytest学习总结3.3 – pytest.ini、API解读

3.3.1 命令行选项

您可以通过使用通用帮助选项获得有关ini样式配置文件中的命令行选项和值的帮助:
pytest -h 这将显示由已安装的插件注册的命令行和配置文件设置。

3.3.2 配置文件格式

许多 pytest 设置可以在配置文件中设置,按照惯例,该配置文件位于存储库的根目录中。 pytest.ini文件优先于其他文件,即使是空的。


[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

其他配置文件了解下:


[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
    "tests",
    "integration", ]

[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

3.3.3 – 5 不深入

本页面包含了对pytest的API的完整引用

3.4.1 方法 – Functions:

  • pytest.approx
from pytest import approx
if 0.1 + 0.2 == approx(0.3):
    print("通过1")
if {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6}):
    print("通过2")
  • pytest.fail(reason=’失败的理由’)
  • pytest.skip(reason=’跳过的理由’)
  • pytest.importorskip :如果无法导入模块,则跳过当前测试
  • pytest.xfail : 必须使用给定的原因 xfail 正在执行的测试或设置功能,此函数应仅在测试期间调用(设置、调用或拆卸)。
  • pytest.exit :退出测试过程
  • pytest.main : 执行进程中的测试
  • pytest.param
import pytest
@pytest.mark.parametrize(
    "test_input,expected",
    [
        ("3+5", 8),
        pytest.param("6*9", 42, marks=pytest.mark.xfail),
    ],
)
def test_eval(test_input, expected):
    assert eval(test_input) == expected
  • pytest.raises
import pytest
value = 15
with pytest.raises(ValueError) as exc_info:
    if value > 10:
        raise ValueError("value must be )
    assert exc_info.type is ValueError
  • pytest.deprecated_call
  • pytest.register_assert_rewrite
  • pytest.warns
  • pytest.freeze_includes

3.4.2 标记 – Marks

  • pytest.mark.filterwarnings
  • pytest.mark.parametrize
  • pytest.mark.skip
  • pytest.mark.skipif
  • pytest.mark.usefixtures
  • pytest.mark.xfail
  • Custom marks

3.4.3 内置的固定装置 – fixture

  • @pytest.fixture
  • config.cache
  • capsys
  • capsysbinary
  • capfd
  • capfdbinary
  • doctest_namespace
  • request
  • pytestconfig
  • record_property
  • record_testsuite_property
  • caplog
  • monkeypatch
  • pytester
  • testdir
  • recwarn
  • tmp_path
  • tmp_path_factory
  • tmpdir
  • tmpdir_factory

3.4.4 内置钩子函数 – Hooks

  • Bootstrapping hooks
  • Initialization hooks
  • Collection hooks
  • Test running (runtest) hooks
  • Reporting hooks
  • Debugging/Interaction hooks

3.4.5 全局变量

当在测试模块或 conftest.py 文件中定义时,pytest 以特殊方式处理一些全局变量。

collect_ignore = ["setup.py"]
collect_ignore_glob = ["*_ignore.py"]
pytest_plugins = "myapp.testsupport.myplugin"
pytest_plugins = ("myapp.testsupport.tools", "myapp.testsupport.regression")
import pytest
pytestmark = pytest.mark.webtest
pytestmark = [pytest.mark.integration, pytest.mark.slow]

3.4.5 配置选项

1. addopts


[pytest]
addopts = --maxfail=2 -rf

实际上是这样运行的: pytest --maxfail=2 -rf test_hello.py

2. pytest的所有参数配置


[pytest]
addopts = --maxfail=2 -rf
console_output_style = classic
empty_parameter_set_mark = xfail
faulthandler_timeout=5
filterwarnings =
    error
    ignore::DeprecationWarning
junit_duration_report = call
junit_family = xunit2
junit_logging = system-out
junit_log_passing_tests = False
junit_suite_name = my_suite
log_auto_indent = False
log_cli = True
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = INFO
log_date_format = %Y-%m-%d %H:%M:%S
log_file = logs/pytest-logs.txt
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file_format = %(asctime)s %(levelname)s %(message)s
log_file_level = INFO
log_format = %(asctime)s %(levelname)s %(message)s
log_level = INFO

addopts = --strict-markers
markers =
    slow
    serial

norecursedirs = .svn _build tmp*

python_classes = *Suite
python_files = test_*.py check_*.py example_*.py
python_files =
    test_*.py
    check_*.py
    example_*.py
python_functions = *_test
pythonpath = src1 src2

required_plugins = pytest-django>=3.0.0,<4.0.0 pytest-html pytest-xdist>=1.0.0
testpaths = testing doc
usefixtures =
    clean_db
xfail_strict = True

3.4.6 – 10 【不深入】

Original: https://blog.csdn.net/weixin_45451320/article/details/124183915
Author: 阿_焦
Title: pytest学习总结3.3 – pytest.ini、API解读

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772871/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Pygame Time时间控制详解

    Pygame Time时间控制详解 pygame.time 时间控制模块,是 Pygame 中使用频率较高的模块,其主要功能是管理时间和游戏帧数率(即 FPS)。 时间在游戏开发中…

    Python 2023年9月20日
    061
  • 【python】词云图制作

    词云图制作 python 练了一段时间的词云图,就来和大家讲讲词云图制作的详细过程。 效果图 ; 工具准备 1、python3 2、安装第三方库wordcloud 3、安装nump…

    Python 2023年8月1日
    067
  • pytest学习总结2.13 – skip、xfail使用全集

    标记不能在某些平台上运行或您希望失败的测试函数,以便pytest可以相应地处理它们,并呈现测试会话的摘要,同时保持测试套件的绿色,跳过意味着您只有在满足某些条件时才期望测试通过,否…

    Python 2023年9月11日
    050
  • Python加载数据的5种方法

    我们回顾了五种引入数据的Python技术,并附有代码实例供你参考。本文作者是 Ahmad Anis,他是一位机器学习和数据科学的学生。今年4月15日发表于Python。 关注《Py…

    Python 2023年8月27日
    061
  • 【FFmpeg】学会添加水印,只要这一篇就足够

    打算写这样一篇文章很久了,算是对过往的一种总结,也希望能获得更多的反馈继续迭代。在这个人类的主要信息载体已经变为视频的年代,水印的添加也成为了一个许多人不可或缺的技能,对于技术人来…

    Python 2023年9月16日
    090
  • Python Web开发(六):前后端分离的架构

    本文目录: 一、代码直接生成HTML 二、使用模板 三、前后端分离架构 【系列好文推荐】 &#x524D;&#x8A00;&#xFF1A; 📢📢📢🏅🏅🏅作者…

    Python 2023年8月9日
    056
  • 洞察市场需求,深耕大健康赛道,缤跃酒店打造一站式运动酒店品牌

    近期,人民数据研究院发布《2022全民跑步运动健康报告》,报告中显示参与跑步人群的年龄跨度随着社会对跑步运动不断攀升的热情而增加。现代生活节奏加快、竞争压力大使得部分中青年通过运动…

    Python 2023年10月10日
    055
  • 周末福利!用Python爬取美团美食信息,吃货们走起来!

    如果你不在一个盛大的周末款待自己,你怎么能不辜负一周的辛勤工作,对吗? [En] If you don’t treat yourself on a big weeken…

    Python 2023年5月25日
    092
  • pandas之查找数据

    由于Series数据结构较为简单,因此先主要介绍DataFrame数据结构的查找数据。 先介绍按列查找数据的方法。 2.1 查找一整列的数据 先创建一个DataFrame data…

    Python 2023年8月6日
    053
  • 【算法分析与设计】【期中(末)复习题】【2022秋】

    文章目录 一. 单选题 二. 填空题 三. 判断题 四. 多选题 一. 单选题 1.按照渐近阶从低到高的顺序排列下列表达式: 30n,2logn,4,n! A. 4 Origina…

    Python 2023年9月26日
    054
  • .NET周报【11月第3期 2022-11-22】

    国内文章 .NET Conf China 2022 第一批讲师阵容大揭秘!整个期待了! https://mp.weixin.qq.com/s/4p89hhBPw6qv-0OB_T_…

    Python 2023年10月14日
    0114
  • Python全局变量跨模块变量定义和使用

    在同一个py脚本中,定义在所有函数之外的变量可以理解为全局变量,因为所有函数可以直接访问函数外的变量(但不能访问其他函数的私有变量),这里涉及到变量的作用域,一般分为函数作用域和全…

    Python 2023年8月23日
    053
  • scrapy命令和项目调试-scrapy框架4-python

    在编写项目的时候,需要不断的调试代码。同时频繁大量的请求目标网站,可能触发一些安全策略,比如屏蔽IP等等。这时,需要掌握一些调试技巧。在这之前,先了解一些scrapy命令。 scr…

    Python 2023年10月1日
    055
  • 使用GPU运行python项目

    简单科普:CPU适合串行计算,擅长逻辑控制。GPU擅长并行高强度并行计算,适用于AI算法的训练学习GPU教为侧重于运算,因此GPU常被用于一些深度学习的项目,要想使用GPU来运行深…

    Python 2023年8月1日
    061
  • Python ttkbootstrap 制作账户注册信息界面

    前言 ttkbootstrap 是一个基于 tkinter 的界面美化库,使用这个工具可以开发出类似前端 bootstrap 风格的 tkinter 桌面程序。ttkbootstr…

    Python 2023年11月9日
    050
  • scrapy怎么连接mysql_Scrapy连接到各类数据库(SQLite,Mysql,Mongodb,Redis)

    如何使用scrapy连接到(SQLite,Mysql,Mongodb,Redis)数据库,并把爬取的数据存储到相应的数据库中。 一、SQLite 1.修改pipelines.py文…

    Python 2023年10月3日
    041
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球