返回: Pytest权威教程

缓存:使用跨执行状态

版本2.8中的新函数。

使用方法

该插件提供了两个命令行选项,用于重新运行上次pytest调用的失败:

  • --lf,--last-failed- 只重新运行故障。
  • --ff,--failed-first- 先运行故障然后再运行其余的测试。

对于清理(通常不需要),--cache-clear选项允许在测试运行之前删除所有跨会话缓存内容。

其他插件可以访问[config.cache对象以在调用之间设置/获取json可编码pytest

注意

此插件默认启用,但如果需要可以禁用:请参阅[按名称取消激活/取消注册插件(此插件的内部名称为cacheprovider)。

首先只重新运行故障或故障

首先,让我们创建50个测试调用,其中只有2个失败:

# content of test_50.py
import pytest @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
pytest.fail("bad luck")

如果你是第一次运行它,你会看到两个失败:

$ pytest -q
.................F.......F........................ [100%]
================================= FAILURES =================================
_______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
_______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
2 failed,48 passed in 0.12 seconds

如果你然后运行它--lf

$ pytest --lf
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 50 items / 48 deselected / 2 selected
run-last-failure: rerun previous 2 failures test_50.py FF [100%] ================================= FAILURES =================================
_______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
_______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
================= 2 failed,48 deselected in 0.12 seconds ==================

你只运行了上次运行中的两个失败测试,​​而尚未运行48个测试(“取消选择”)。

现在,如果使用该--ff选项运行,将运行所有测试,但首先执行先前的失败(从一系列FF和点中可以看出):

$ pytest --ff
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 50 items
run-last-failure: rerun previous 2 failures first test_50.py FF................................................ [100%] ================================= FAILURES =================================
_______________________________ test_num[17] _______________________________ i = 17 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
_______________________________ test_num[25] _______________________________ i = 25 @pytest.mark.parametrize("i",range(50))
def test_num(i):
if i in (17,25):
> pytest.fail("bad luck")
E Failed: bad luck test_50.py:6: Failed
=================== 2 failed,48 passed in 0.12 seconds ====================

新的--nf,--new-first选项:首先运行新的测试,然后是其余的测试,在这两种情况下,测试也按文件修改时间排序,最新的文件首先出现。

上次运行中没有测试失败时的行为

如果在上次运行中没有测试失败,或者没有lastfailed找到缓存数据,pytest则可以使用该--last-failed-no-failures选项配置运行所有测试或不运行测试,该选项采用以下值之一:

pytest --last-failed --last-failed-no-failures all    # run all tests (default behavior)
pytest --last-failed --last-failed-no-failures none # run no tests and exit

新的config.cache对象

插件或conftest.py支持代码可以使用pytestconfig对象获取缓存值。这是一个实现[pytest fixture的基本示例插件[:显式,模块化,可伸缩,它在pytest调用中重用以前创建的状态:

# content of test_caching.py
import pytest
import time def expensive_computation():
print("running expensive computation...") @pytest.fixture
def mydata(request):
val = request.config.cache.get("example/value",None)
if val is None:
expensive_computation()
val = 42
request.config.cache.set("example/value",val)
return val def test_function(mydata):
assert mydata == 23

如果你是第一次运行此命令,则可以看到print语句:

$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________ mydata = 42 def test_function(mydata):
> assert mydata == 23
E assert 42 == 23 test_caching.py:17: AssertionError
-------------------------- Captured stdout setup ---------------------------
running expensive computation...
1 failed in 0.12 seconds

如果再次运行它,将从缓存中检索该值,并且不会打印任何内容:

$ pytest -q
F [100%]
================================= FAILURES =================================
______________________________ test_function _______________________________ mydata = 42 def test_function(mydata):
> assert mydata == 23
E assert 42 == 23 test_caching.py:17: AssertionError
1 failed in 0.12 seconds

有关更多详细信息,请参阅[config.cache。

检查缓存内容

你始终可以使用--cache-show命令行选项查看缓存的内容:

$ pytest --cache-show
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
cachedir: $PYTHON_PREFIX/.pytest_cache
------------------------------- cache values -------------------------------
cache/lastfailed contains:
{'test_50.py::test_num[17]': True,
'test_50.py::test_num[25]': True,
'test_assert1.py::test_function': True,
'test_assert2.py::test_set_comparison': True,
'test_caching.py::test_function': True,
'test_foocompare.py::test_compare': True}
cache/nodeids contains:
['test_caching.py::test_function']
cache/stepwise contains:
[]
example/value contains:
42 ======================= no tests ran in 0.12 seconds =======================

清除缓存内容

你可以通过添加如下--cache-clear选项来指示pytest清除所有缓存文件和值:

pytest --cache-clear

对于Continuous Integration服务器的调用,建议使用此选项,其中隔离和正确性比速度更重要。

逐步修复失败用例

作为替代方案,尤其是对于你希望测试套件的大部分都会失败的情况,允许你一次修复一个。测试套件将运行直到第一次失败然后停止。在下次调用时,测试将从上次失败测试继续,然后运行直到下一次失败测试。你可以使用该选项忽略一个失败的测试,并在第二个失败的测试中停止测试执行。如果你遇到失败的测试而只是想稍后忽略它,这将非常有用。--lf-x``--sw``--stepwise``--stepwise-skip

unittest.TestCase支持

Pytest支持unittest开箱即用的基于Python的测试。它旨在利用现有unittest的测试套件将pytest用作测试运行器,并允许逐步调整测试套件以充分利用pytest的函数。

要使用运行现有unittest样式的测试套件pytest,请键入:

pytest tests

pytest将自动收集unittest.TestCase子类及其test方法test_*.py*_test.py文件。

几乎所有unittest函数都受支持:

  • @unittest.skip风格装饰;
  • setUp/tearDown;
  • setUpClass/tearDownClass;
  • setUpModule/tearDownModule;

到目前为止,pytest不支持以下函数:

  • load_tests协议;
  • 分测验;

开箱即用的好处

通过使用pytest运行测试套件,你可以使用多种函数,在大多数情况下无需修改现有代码:

  • 获得[更多信息性的追溯;
  • stdout和stderr捕获;
  • ;
  • 在第一次(或N次)故障后停止;
  • -pdb);
  • 使用[pytest-xdist插件将测试分发给多个CPU;
  • 使用[普通的assert-statements对此非常有帮助);

unittest.TestCase子类中的pytest特性

以下pytest函数适用于unittest.TestCase子类:

  • 标记: skip,[skipif,[xfail;
  • 自动使用Fixture方法;

下面pytest函数工作,也许永远也因不同的设计理念:

  • Fixture方法);
  • 参数化;
  • 定制挂钩;

第三方插件可能运行也可能不运行,具体取决于插件和测试套件。

unittest.TestCase 使用标记将pytestFixture方法混合到子类中

运行unittestpytest允许你使用其[Fixture方法机制进行unittest.TestCase样式测试。假设你至少浏览了pytest fixture函数,让我们跳转到一个集成pytestdb_classfixture,设置类缓存数据库对象,然后从unittest样式测试中引用它的示例如:

# content of conftest.py

# we define a fixture function below and it will be "used" by
# referencing its name from tests import pytest @pytest.fixture(scope="class")
def db_class(request):
class DummyDB(object):
pass
# set a class attribute on the invoking test context
request.cls.db = DummyDB()

这定义了一个fixture函数db_class- 如果使用的话 - 为每个测试类调用一次,并将class-leveldb属性设置为一个DummyDB实例。fixture函数通过接收一个特殊request对象来实现这一点,该对象允许访问[请求测试上下文,例如cls属性,表示使用该fixture的类。该架构将Fixture方法写入与实际测试代码分离,并允许通过最小参考(Fixture方法名称)重新使用Fixture方法。那么让unittest.TestCase我们使用fixture定义编写一个实际的类:

# content of test_unittest_db.py

import unittest
import pytest @pytest.mark.usefixtures("db_class")
class MyTest(unittest.TestCase):
def test_method1(self):
assert hasattr(self,"db")
assert 0,self.db # fail for demo purposes def test_method2(self):
assert 0,self.db # fail for demo purposes

@pytest.mark.usefixtures("db_class")类的装饰可确保pytest固定函数db_class被调用每一次班。由于故意失败的断言语句,我们可以看看self.db回溯中的值:

$ pytest test_unittest_db.py
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 2 items test_unittest_db.py FF [100%] ================================= FAILURES =================================
___________________________ MyTest.test_method1 ____________________________ self = <test_unittest_db.MyTest testMethod=test_method1> def test_method1(self):
assert hasattr(self,"db")
> assert 0,self.db # fail for demo purposes
E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
E assert 0 test_unittest_db.py:9: AssertionError
___________________________ MyTest.test_method2 ____________________________ self = <test_unittest_db.MyTest testMethod=test_method2> def test_method2(self):
> assert 0,self.db # fail for demo purposes
E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
E assert 0 test_unittest_db.py:12: AssertionError
========================= 2 failed in 0.12 seconds =========================

这个默认的pytest回溯显示两个测试用例共享同一个self.db实例,这是我们在编写上面的类范围的fixture函数时的意图。

使用autouseFixture方法和访问其他Fixture方法

虽然通常更好地明确声明对给定测试需要使用的Fixture方法,但有时你可能想要在给定的上下文中自动使用Fixture方法。毕竟,传统的unittest-setup风格要求使用这种隐含的Fixture方法编写,而且很有可能,你习惯它或者喜欢它。

你可以使用标记Fixture方法函数@pytest.fixture(autouse=True)并在要使用它的上下文中定义Fixture方法函数。让我们看一个initdirFixture方法,它使一个TestCase类的所有测试用例都在一个预先初始化的临时目录中执行samplefile.ini。我们的initdirfixture本身使用pytest builtin[tmpdirfixture来委托创建一个per-test临时目录:

# content of test_unittest_cleandir.py
import pytest
import unittest class MyTest(unittest.TestCase): @pytest.fixture(autouse=True)
def initdir(self,tmpdir):
tmpdir.chdir() # change to pytest-provided temporary directory
tmpdir.join("samplefile.ini").write("# testdata") def test_method(self):
with open("samplefile.ini") as f:
s = f.read()
assert "testdata" in s

由于该autouse标志,initdirfixture函数将用于定义它的类的所有方法。这是@pytest.mark.usefixtures("initdir")在类中使用标记的快捷方式,如上例所示。

运行此测试模块......:

$ pytest -q test_unittest_cleandir.py
. [100%]
1 passed in 0.12 seconds

...给我们一个通过测试,因为initdirFixture方法函数在之前执行test_method

注意

unittest.TestCase方法不能直接接收fixture参数作为实现可能会导致运行通用unittest.TestCase测试套件的能力。

以上usefixturesautouse示例应该有助于将pytestFixture方法混合到unittest套件中。

你也可以逐步从子类化转移unittest.TestCase普通断言,然后开始逐步从完整的pytest函数集中受益。

注意

unittest.TestCase子类运行测试--pdb将禁用针对发生异常的情况的tearDown和cleanup方法。这允许对所有在其tearDown机器中具有重要逻辑的应用程序进行适当的事后调试。但是,支持此函数会产生以下副作用:如果人们覆盖unittest.TestCase``__call__或者run需要以debug相同的方式覆盖(对于标准unittest也是如此)。

注意

由于两个框架之间的架构差异,在unittest测试call阶段而不是在pytest标准setupteardown阶段中执行基于测试的设置和拆卸。在某些情况下,这一点非常重要,特别是在推理错误时。例如,如果unittest基于a的套件在设置期间出现错误,pytest则在其setup阶段期间将报告没有错误,并且将在此期间引发错误call

Pytest权威教程14-缓存:使用跨执行状态的更多相关文章

  1. Pytest权威教程(官方教程翻译)

    Pytest权威教程01-安装及入门 Pytest权威教程02-Pytest 使用及调用方法 Pytest权威教程03-原有TestSuite的执行方法 Pytest权威教程04-断言的编写和报告 P ...

  2. Pytest权威教程21-API参考-03-夹具(Fixtures)

    目录 夹具(Fixtures) @ pytest.fixture config.cache的 capsys capsysbinary capfd capfdbinary doctest_namespa ...

  3. Pytest权威教程25-配置

    目录 配置 命令行选项和配置文件设置 初始化:确定ROOTDIR和INIFILE 寻找rootdir 如何更改命令行选项默认值 内置的配置文件选项 返回: Pytest权威教程 配置 命令行选项和配置 ...

  4. Pytest权威教程21-API参考-07-配置选项(Configuration Options)

    目录 配置选项(Configuration Options) addopts cache_dir confcutdir console_output_style doctest_encoding do ...

  5. Pytest权威教程21-API参考-04-钩子(Hooks)

    目录 钩子(Hooks) 引导时的Hook方法 初始化时的Hook方法 测试运行时的Hook方法 收集用例时的Hook方法 生成测试结果时的Hook方法 调试/交互Hook方法 返回: Pytest权 ...

  6. Pytest权威教程02-Pytest 使用及调用方法

    目录 Pytest 使用及调用方法 使用python -m pytest调用pytest 可能出现的执行退出code 获取版本路径.命令行选项及环境变量相关帮助 第1(N)次失败后停止测试 指定及选择 ...

  7. Pytest权威教程05-Pytest fixtures:清晰 模块化 易扩展

    目录 Pytest fixtures:清晰 模块化 易扩展 Fixtures作为函数参数使用 Fixtures: 依赖注入的主要例子 conftest.py: 共享fixture函数 共享测试数据 生 ...

  8. Pytest权威教程20-日志

    目录 记录日志 caplog Fixture方法 实时日志 版本改动记录 Pytest3.4中不向后兼容的更改 返回: Pytest权威教程 记录日志 Pytest默认捕获WARNING以上日志消息, ...

  9. Pytest权威教程19-编写钩子(Hooks)方法函数

    目录 编写钩子(Hooks)函数 钩子函数验证和执行 firstresult: 遇到第一个有效(非None)结果返回 hookwrapper:在其他钩子函数周围执行 钩子(Hooks)函数排序/调用示 ...

随机推荐

  1. 前端开发 Angular

    https://www.angularjs.net.cn/tutorial/18.html

  2. ElementUI 源码定制防坑指南

    背景 我司OA系统公文管理模块Office在线编辑使用的是金格IWebOffice中间件[PPAPI插件,通过<object>标签加载],IWebOffice在chrome中设置div盒子 ...

  3. 【一起学源码-微服务】Netflix Eureka 源码一:Netflix Eureka 源码初探,我们为什么要读源码?

    前言 最近发现 网上好多自己的博客,很多朋友转载了文章却不加下 原载地址,本文欢迎转载一起学习,请在目录出加上原出处,感谢.转载来自:博客(一枝花算不算浪漫) 看了前面几篇文章的小伙伴知道,前几天在学 ...

  4. Flask之基础

    一,flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...

  5. 191010 python3分解质因数

    # 题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.# 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:# (1)如果这个质数恰等于n,则说明分解 ...

  6. visual studio故障修复

    如果没有安装程序,直接在控制面板——>程序和功能,在列表中找到您安装的vs,右键选择更改,然后程序会启动,做一些准备.然后又三个选项,可以选择修复.

  7. IDEA实用教程(十)—— 配置Maven的全局设置

    使用之前需要提前安装好Maven 第一步 第二步

  8. Kotlin构造方法详解与初始化过程分析

    在上一次https://www.cnblogs.com/webor2006/p/11192025.html已经初识了Kolin的面向对象,先来回顾一下: 介绍了primary 构造方法,其特点回忆一下 ...

  9. linux系统编程之信号(二)

    经过了漫长的间歇,对于c语言的学习也被中断了很久,现实确实有很多的无耐,计划中的事情总会被打乱,但不管怎样,学习的道路是不能休止的,所以经过了一断温习后现在继续学习C语言,话不多说,进入正题: 信号分 ...

  10. RSA 加密 解密 (长字符串) JAVA JS版本加解密

    系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...