前言

零基础1小时快速入门pytest自动化测试教程,全套项目框架实战

pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行

非test文件

pytest里面有些文件是非test文件

  • pytest.ini:pytest的主配置文件,可以改变pytest的默认行为
  • conftest.py:测试用例的一些fixture配置
  • _init_.py:识别该文件夹为python的package包

查看pytest.ini的配置选项

cmd执行

pytest --help

找到这部分内容

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:markers (linelist):   markers for test functionsempty_parameter_set_mark (string):default marker for empty parametersetsnorecursedirs (args): directory patterns to avoid for recursiontestpaths (args):     directories to search for tests when no files or directories are given in the command line.usefixtures (args):   list of default fixtures to be used with this projectpython_files (args):  glob-style file patterns for Python test module discoverypython_classes (args):prefixes or glob names for Python test class discoverypython_functions (args):prefixes or glob names for Python test function and method discoverydisable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):disable string escape non-ascii characters, might cause unwanted side effects(use at your ownrisk)console_output_style (string):console output: "classic", or with additional progress information ("progress" (percentage) |"count").xfail_strict (bool):  default for the strict parameter of xfail markers when not given explicitly (default: False)enable_assertion_pass_hook (bool):Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cachefiles.junit_suite_name (string):Test suite name for JUnit reportjunit_logging (string):Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|alljunit_log_passing_tests (bool):Capture log information for passing tests to JUnit report:junit_duration_report (string):Duration time to report: one of total|calljunit_family (string):Emit XML for schema: one of legacy|xunit1|xunit2doctest_optionflags (args):option flags for doctestsdoctest_encoding (string):encoding used for doctest filescache_dir (string):   cache directory path.filterwarnings (linelist):Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.log_print (bool):     default value for --no-print-logslog_level (string):   default value for --log-levellog_format (string):  default value for --log-formatlog_date_format (string):default value for --log-date-formatlog_cli (bool):       enable log display during test run (also known as "live logging").log_cli_level (string):default value for --log-cli-levellog_cli_format (string):default value for --log-cli-formatlog_cli_date_format (string):default value for --log-cli-date-formatlog_file (string):    default value for --log-filelog_file_level (string):default value for --log-file-levellog_file_format (string):default value for --log-file-formatlog_file_date_format (string):default value for --log-file-date-formatlog_auto_indent (string):default value for --log-auto-indentfaulthandler_timeout (string):Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Notavailable on Windows.addopts (args):       extra command line optionsminversion (string):  minimally required pytest versionrsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.rsyncignore (pathlist):list of (relative) glob-style paths to be ignored for rsyncing.looponfailroots (pathlist):directories to check for changes

pytest.ini应该放哪里?

就放在项目根目录下 ,不要乱放,不要乱起其他名字

接下来讲下常用的配置项

marks

作用:测试用例中添加了 @pytest.mark.webtest 装饰器,如果不添加marks选项的话,就会报warnings

格式:list列表类型

写法:

[pytest]
markers =weibo: this is weibo pagetoutiao: toutiaoxinlang: xinlang

xfail_strict

作用:设置xfail_strict = True可以让那些标记为@pytest.mark.xfail但实际通过显示XPASS的测试用例被报告为失败

格式:True 、False(默认),1、0

写法:

[pytest]# mark标记说明
markers =weibo: this is weibo pagetoutiao: toutiaoxinlang: xinlangxfail_strict = True

具体代码栗子

未设置 xfail_strict = True 时,测试结果显示XPASS

@pytest.mark.xfail()
def test_case1():a = "a"b = "b"assert a != b

collecting ... collected 1 item

02断言异常.py::test_case1 XPASS [100%]

============================= 1 xpassed in 0.02s ==============================

已设置 xfail_strict = True 时,测试结果显示failed

collecting ... collected 1 item02断言异常.py::test_case1 FAILED                                         [100%]
02断言异常.py:54 (test_case1)
[XPASS(strict)] ================================== FAILURES ===================================
_________________________________ test_case1 __________________________________
[XPASS(strict)] 
=========================== short test summary info ===========================
FAILED 02断言异常.py::test_case1
============================== 1 failed in 0.02s ==============================

addopts

作用:addopts参数可以更改默认命令行选项,这个当我们在cmd输入一堆指令去执行用例的时候,就可以用该参数代替了,省去重复性的敲命令工作

比如:想测试完生成报告,失败重跑两次,一共运行两次,通过分布式去测试,如果在cmd中写的话,命令会很长

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

每次都这样敲不太现实,addopts就可以完美解决这个问题

[pytest]# mark
markers =weibo: this is weibo pagetoutiao: toutiaoxinlang: xinlangxfail_strict = True# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

加了addopts之后,我们在cmd中只需要敲pytest就可以生效了!!

log_cli

作用:控制台实时输出日志

格式:log_cli=True 或False(默认),或者log_cli=1 或 0

log_cli=0的运行结果

log_cli=1的运行结果

结论

很明显,加了log_cli=1之后,可以清晰看到哪个package下的哪个module下的哪个测试用例是否passed还是failed;

所以平时测试代码是否有问题的情况下推荐加!!!但如果拿去批量跑测试用例的话不建议加,谁知道会不会影响运行性能呢?

norecursedirs

作用:pytest 收集测试用例时,会递归遍历所有子目录,包括某些你明知道没必要遍历的目录,遇到这种情况,可以使用 norecursedirs 参数简化 pytest 的搜索工作【还是挺有用的!!!】

默认设置: norecursedirs = .* build dist CVS _darcs {arch} *.egg 

正确写法:多个路径用空格隔开

[pytest]norecursedirs = .* build dist CVS _darcs {arch} *.egg venv src resources log report util

更改测试用例收集规则

pytest默认的测试用例收集规则

  • 文件名以 test_*.py 文件和 *_test.py
  • 以  test_ 开头的函数
  • 以  Test 开头的类,不能包含 __init__ 方法
  • 以  test_ 开头的类里面的方法

我们是可以修改或者添加这个用例收集规则的;当然啦,是建议在原有的规则上添加的,如下配置

[pytest]python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/922320.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/922320.shtml
英文地址,请注明出处:http://en.pswp.cn/news/922320.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

硬件开发2-ARM裸机开发3-IMX6ULL - 引入中断

一、铺垫引入中断 → 按键1、概要:实现按键控制发光二极管和蜂鸣器输入类型的外设:按键(key)2、参考手册内容完成配置过程(1)key 按键原理图(2)core 内核中命名 -- UART1 CTS&#x…

Ansible的 Playbook 模式详解

目录一、Playbook模式1.1 Playbook 的优势1.2 Playbook 的组成1.3 安装 httpd 服务案例1.4 Playbook 命令及常用参数1.5 Playbook 的语法 —— 权限相关1. remote_user2. become3. become_method1.6 Playbook 的通知与触发机制1. notify2. handlers3. 使用示例4. 使用场景1.6 P…

猿辅导Java后台开发面试题及参考答案

int 与 Integer 的区别是什么?若创建数量庞大的数字时使用 Integer,会对重复数字创建新对象吗?int 是 Java 中的基本数据类型,直接存储数值,占用 4 个字节,默认值为 0,不需要通过 new 关键字创建…

代码随想录学习摘抄day9(回溯1-11)

一个朴实无华的目录定义:回溯法也可以叫做回溯搜索法,它是一种搜索的方式。应用场景:回溯法解决的问题都可以抽象为树形结构代码模板题型第77题. 组合思路:每次从集合中选取元素,可选择的范围随着选择的进行而收缩&…

Altium Designer(AD24)打开工程文件的几种方法

🏡《专栏目录》 目录 1,概述 2,源文件 2,菜单栏 4,工具栏 5,注意事项 1,概述 本文介绍几种打开工程文件的方法。 2,源文件 找到工程的源文件存储路径,找到.PrjPcb的源工程文件,双击打开。 2,菜单栏 第1步:执行File→Open, 第2步:找到工程文件的存储路径,并选中…

Linux嵌入式自学笔记(基于野火EBF6ULL):2.点灯与ubuntu安装

一、点灯登录root:账号:root ; 密码:root点灯命令:echo 0 > /sys/class/leds/red/brightness #关闭red灯 echo 0 > /sys/class/leds/blue/brightness #关闭blue灯 echo 0 > /sys/class/leds/green/brightness …

【Java实战㊷】Java实战:MyBatis-Plus 开启MySQL数据库高效操作之旅

目录 一、MyBatis-Plus 环境集成 1.1 项目依赖引入 1.2 数据库配置 1.3 代码生成器使用 二、核心 CRUD 操作实现 2.1 基础查询 2.2 数据新增与修改 2.3 复杂查询场景 三、性能优化与高级特性 3.1 缓存配置 3.2 乐观锁实现 3.3 字段自动填充 四、实战案例:用户管理模块开发 4.1…

开学季干货——知识梳理与经验分享

技术文章大纲:开学季干货——知识梳理与经验分享目标受众分析明确文章面向的学生群体(如大学生、高中生) 分析不同群体的核心需求(课程准备、时间管理、工具使用) 结合技术场景(如数字笔记、在线协作&#…

Linux《线程(上)》

通过之前的学习我们已经了解了操作系统当中的基本的概念包括进程、基础IO、磁盘文件存储等,但是到目前为止我们还未了解到线程相关的概念,这就使得当前我们对操作系统的认知还不是完整的,现在我们是还是无法理解一个进程当中是如何同时的执行…

为什么知识复用时缺乏场景化指导影响实用性

知识复用时因缺乏场景化指导而严重影响实用性,其根本原因在于知识的价值本质上根植于其应用情境。脱离了场景的“纯知识”往往是抽象、片面且难以行动的。这导致了认知鸿沟的产生、隐性知识的流失、决策风险的增加、以及学习迁移效率的低下。当使用者面对一份缺乏“…

拥抱直觉与创造力:走进VibeCoding的新世界

引言 在传统观念里,编程是一项高度理性、逻辑严密的活动,开发者需要像建筑师一样,用代码一行行地精确构建数字世界。然而,随着人工智能技术的飞速发展,一种全新的编程理念和体验正在兴起——它就是 VibeCoding&#xf…

HTTP的Web服务测试在Python中的实现

在Web开发领域,对HTTP Web服务进行测试是确保服务稳定性和可靠性的关键步骤。Python作为一种功能强大的编程语言,提供了多种工具和库来简化这一过程。本文将介绍如何在Python中实现HTTP的Web服务测试。首先,Python的requests库是测试HTTP Web…

Android Studio 构建项目时 Gradle 下载失败的解决方案

一、问题原因分析根据错误日志:下载地址 https://services.gradle.org/distributions/gradle-8.1-bin.zip 连接超时(10秒)。可能原因:网络环境限制(如公司防火墙、地区网络屏蔽)。代理配置未生效或配置错误…

mysql 与 MongoDB 的分片

MySQL 和 MongoDB 作为不同类型数据库的代表(关系型 vs 文档型),其分片机制在设计理念、实现方式和适用场景上存在显著差异。两者的分片核心目标一致——通过水平扩展(Scale Out)解决单节点存储容量和性能瓶颈,但因数据模型、事务支持和分布式设计理念的不同,形成了截然…

Coze源码分析-资源库-创建知识库-前端源码-核心逻辑与接口

创建知识库逻辑 1. 表单验证系统 文件位置:frontend/packages/data/knowledge/knowledge-modal-base/src/create-knowledge-modal-v2/features/add-type-content/coze-knowledge/index.tsx 知识库创建表单的验证规则: // 知识库名称验证规则 const nameV…

欧拉函数 | 定义 / 性质 / 应用

注:本文为 “欧拉函数” 相关合辑。 略作重排,未整理去重。 如有内容异常,请看原文。 欧拉函数最全总结 jiet07 已于 2024-10-22 10:00:54 修改 一、欧拉函数的引入 首先引入互质关系: 如果两个正整数,除了 111 以…

ubuntu git push每次都要输入密码怎么解决只输入一次密码

在 Ubuntu 下使用 Git 时,如果每次 push 都需要重复输入密码,可以通过配置 Git 凭证存储来解决。以下是几种常用方法: 🔑 方法一:使用 Git 凭证缓存(推荐) 设置凭证缓存(默认 15 分钟…

【机械故障】使用fir滤波器实现数据拟合

使用fir滤波器实现数据拟合 提示:学习笔记 使用fir滤波器实现数据拟合使用fir滤波器实现数据拟合一、问题建模二、 构建矩阵方程(关键步骤)三、最小二乘解四、重要注意事项4.1 滤波器长度 M4.2 数据的预处理4.3 延迟问题4.4 性能评估一、问题…

STC8H系列-高级PWM-两相步进电机-细分驱动

两相步进电机, STC8H系列 用高级PWM实现SPWM细分驱动 /************* 功能说明 ************** 用B组高级PWM细分驱动2相4线小型步进电机, 支持1、2、4、8、16、32、64细分, 比如1.8度的电机4细分到0.45度. 本程序用于演示SPWM多细分直接驱动2相4线小型步进电机…

内网环境下ubuntu 20.04搭建深度学习环境总结

2025年9月更新,随着人工智能的发展,现在深度学习环境配置越来越简单了,常用的pytorch、paddle(3.x)等深度学习库安装的时候自带了cuda和cudnn的python包,不需要在操作系统层面自己安装,配置环境…