pytest相关问题解析
1. 如果你想查询在你的环境下有哪些pytest的active plugin可以使用:
- py.test --traceconfig
会得到一个扩展的头文件名显示激活的插件和他们的名字。同时也会打印出当前的plugin,也就是被加载时conftest.py文件。
2. pytest.ini文件有什么作用
3. pytest的fixture究竟是怎么工作的,在pytest中它有怎样的作用。
Dealing with fixtures is one of the areas where pytest really shines.
It's to think of fixtures as a set of resources that need to be set up before a test starts, and cleaned up after.
有四种作用域的fixture,分别为function,class,module和session作用域。简单理解function scope,就是每一个函数都会调用;class scope,就是每一个类调用一次,一个类可以有多个函数;module scope,应该是一个文件调用一次,该文件内又有多个function;session scope范围更大,是多个文件调用一次,每个文件有对应着一个module。
| function | Run once per test |
| class | Run once per class of tests |
| module | Run once per module |
| session | Run once per session |
fixture又有autouse的和非autouse的。什么是autouse的呢?就是在定义fixture时autouse为True的,如下:
- @pytest.fixture(scope="session", autouse=True)
在调用时pytest的test函数不需要在参数中指定它也会被调用,如下所示
- def test_alpha_1():
- print('\nIn test_alpha_1()')
非autouse的fixture时没有指定autouse为True的。它在调用时需要显示地写出fixture的名字在pytest函数的参数中,如下:
- @pytest.fixture(scope="session")
- def some_resource(request):
- print('\nIn some_resource()')
- def some_resource_fin():
- print('\nIn some_resource_fin()')
- request.addfinalizer(some_resource_fin)
在调用时需要这么调用
- def test_alpha_2(some_resource):
- print('\nIn test_alpha_2()')
这两者在调用顺序上,在setup阶段,是先调用autouse的,然后再调用非autouse的。在tear down阶段则是反过来,先调用非autouse的,然后再调用autouse的。他们的调用顺序应该是类似于栈的进出栈顺序,先进栈的后出栈,后进栈的先出栈。
查看pytest自带的内在的fixture的方法。
- py.test -q --fixture
pytest中有三种方法去使用一个fixture
- 在test的参数列表中指定。
- 使用usefixtures decorateor
- 使用autouse
4. pytest fixture的一些优势
- 很直观明了地知道哪些tests使用了一个资源,因为这个资源在test的参数列表中。
- 我不用必须 人为地创建类(或者将tests从一个文件移动到另一个),只需要分离fixture应用。
- 对于一个资源来说teardown代码是紧密地和setup代码耦合的。
- 资源的生命周期的范围在资源setup代码的位置处指定。这最终成为一个巨大的优势当你想要摆弄范围来节省测试时间。如果所有的事情开始出现故障,只需要一行的改变去指定函数的范围,让setup/teardown运行围绕着每一个函数/方法
- 更少的代码。pytest的解决方法小于类的解决方法。
- It's obvious which tests are using a resource, as the resource is listed in the test param list.
- I don't have to artificially create classes (or move tests from one file to another) just to separate fixture usage.
- The teardown code is tightly coupled with the setup code for one resource.
- Scope for the lifetime of the resource is specified at the location of the resource setup code. This ends up being a huge benefit when you want to fiddle with scope to save time on testing. If everything starts going haywire, it's a one line change to specify function scope, and have setup/teardown run around every function/method.
- It's less code. The pytest solution is smaller than the class solution.
5.pytest fixture的一些特性
- Return value
- Finalizer is teardown
- Request objects
- Params
6. conftest.py的作用
conftest.py文件是一个单独的存放fixtures的文件。
对于function,class和module来说,把fixture代码放到和test代码同样的文件中完全合理的。但是,对于session,就不再make senese了。
这样的话我们就可以把他们放在conftest.py文件中。这是一个pytest会寻找的一个有特定名字的文件。
7. ini文件查找顺序
pytest默认的ini文件查找顺序为:pytest.ini, tox.ini, setup.cfg。只到第一个[pytest]部分被发现。
例如当我们执行:py.test path/to/testdir时,查找的顺序如下:
- path/to/testdir/pytest.ini
- path/to/testdir/tox.ini
- path/to/testdir/setup.cfg
- path/to/pytest.ini
- path/to/tox.ini
- path/to/setup.cfg
- ... # up until root of filesystem
如果给py.test 提供参数,会从当前的工作路径开始寻找。
转载'https://blog.csdn.net/xibeichengf/article/details/50589235
本文参考了这篇文章
pytest相关问题解析的更多相关文章
- awr相关指标解析
awr相关指标解析 2016年11月11日 15:09
- MAC 相关操作解析
MAC 相关操作解析 OS 显示桌面 f11 F1~F12 fn + F1~F12 撤销重做 command + z command + shift + z 图片预览 选择图片 空格 上下左右 svn ...
- Hadoop NameNode元数据相关文件目录解析
在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...
- SpringBoot应用配置常用相关视图解析器
目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...
- Web 前端性能优化相关内容解析
Web 前端性能优化相关内容,来源于<Google官方网页载入速度检测工具PageSpeed Insights 使用教程>一文中PageSpeed Insights 的相关说明.大家可以对 ...
- Web 前端性能优化相关内容解析[转]
Web 前端性能优化相关内容,来源于<Google官方网页载入速度检测工具PageSpeed Insights 使用教程>一文中PageSpeed Insights 的相关说明.大家可以对 ...
- Hyperledger Fabric相关文件解析
1相关文件说明 这一部分涉及相关配置文件的解析, 网络的启动涉及到多个文件,本文按以下顺序进行分析: . ├── base │ ├── docker-compose-base.yaml #1 │ ...
- Android Https相关完全解析 当OkHttp遇到Https
一.概述 其实这篇文章理论上不限于okhttp去访问自签名的网站,不过接上篇博文了,就叫这个了.首先要了解的事,okhttp默认情况下是支持https协议的网站的,比如https://www.baid ...
- KandQ:单例模式的七种写法及其相关问题解析
设计模式中的单例模式可以有7种写法,这7种写法有各自的优点和缺点: 代码示例(java)及其分析如下: 一.懒汉式 public class Singleton { private static Si ...
随机推荐
- Markdown编辑器editor.md的使用
目录(?)[-] 一Markdown和editormd简介 二editormd的使用 1下载 2简单使用 21在自己的页面上引入相关的css和js代码如下 22在自己的页面中加上DIV 23在同页 ...
- 优化 | Redis AOF重写导致的内存问题 不错
一.问题说明 业务上接到报警提示服务器内存爆了,登录查看发现机器剩余内存还很多,怀疑是被OOM了,查看/var/log/messages: kernel: [25918282.632003] Out ...
- SpringBoot 使用Mybatis+MySql
pom配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- 搜索(BFS)---完美平方数
完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...
- Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板
原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...
- WPF绑定各种数据源之xml数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- RabbitMQ相关使用命令
启动:rabbitmq-server -detached 停止:rabbitmqctl stop 状态:rabbitmqctl status 查看所有用户rabbitmqctl list_users ...
- 阿里P7前端需要哪些技能
原谅我copy过来的,但是这个条理很清楚很有借鉴意义 前言 以下是从公众号的文章中获取到的一位阿里的前端架构师整理的前端架构p7的技能图谱,当然不是最完整.最系统的,所以之后我会一直维护更新这里的内容 ...
- C# 获取系统环境数据
using System; using System.Data; using System.Text.RegularExpressions; using System.Threading; names ...
- python基础--5字典
#字典#dict#基本结构info={ "k1":'v1' #键值对 ,"k2":"v2"} #字典的value可以是任何值# info={ ...