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 ...
随机推荐
- mysql数据库连接 application.properties
# 数据库链接信息mysql.driver=com.mysql.cj.jdbc.Drivermysql.url=jdbc:mysql://localhost:3306/xxxx?characterEn ...
- 自动化测试中,元素无法点击定位等问题的解决:js的使用方法
在自动化测试中经常会遇到使用selenium方法定位元素点击操作失败的情况,例如,我们想实现在浏览器输入http://www.baidu.com,进入百度首页后,鼠标悬停在“更多产品”上,点击“全部产 ...
- java对象的方法属性和代码块的加载顺序
1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性 for example: package com.JavaTest2; publi ...
- ESP32、GPRS A9测试
测试内容: 1.A9作为客户端,在服务器主动断开连接或异常断开的时候,使用网络连接状态查询接口,能否获得准确的网络连接状态. 结果: TCP: A9开多连接时,成功连接TCP服务器后,发送查询语句AT ...
- <每日一题> Day5:简单递推两题
原题链接 参考代码: #include <iostream> using namespace std; typedef long long ll; + ; ll dp[maxn]; int ...
- 固定标签(position: fixed)
document.body.scrollTop 要改成 document.documentElement.scrollTop不然不生效 <!DOCTYPE html> <html l ...
- 利用vsftpd在Linux构建安全的FTP服务
最近在机房搭建Linux环境,需要用到了FTP服务,查看了许多的资料,在这里做一下笔记 一.安装 方法一,使用yum命令安装,需要能够连接外网 # yum install vsftpd 方法二,使用安 ...
- k3 cloud中获取年月日
日期类型字段元素.Date.Year(获取年) 日期类型字段元素.Date.Month(获取月)日期类型字段元素.Date.Day(获取天)
- centos7.4安装mysql
yum -y install mysql mysql-server mysql-devel mysql-server报错:No package mysql-server available,另外两个没 ...
- Hibernate Validation与Spring整合各注解的用法Demo
转自:https://www.aliyun.com/jiaocheng/1315650.html <dependency> <groupId>org.hibernate< ...