• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest自动化框架:权威教程14-缓存:使用跨执行状态

武飞扬头像
测试-八戒
帮助1

缓存:使用跨执行状态

版本2.8中的新函数。

使用方法

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

  • --lf,--last-failed- 只重新运行故障。

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

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

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

注意

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

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

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

  1.  
    Copy# content of test_50.pyimport pytest
  2.  
     
  3.  
    @pytest.mark.parametrize("i",range(50))deftest_num(i):
  4.  
    if i in (17,25):
  5.  
    pytest.fail("bad luck")

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

  1.  
    Copy$ pytest -q
  2.  
    .................F.......F........................ [100%]
  3.  
    ================================= FAILURES =================================
  4.  
    _______________________________ test_num[17] _______________________________
  5.  
     
  6.  
    i = 17
  7.  
     
  8.  
    @pytest.mark.parametrize("i",range(50))
  9.  
    def test_num(i):
  10.  
    if i in (17,25):
  11.  
    > pytest.fail("bad luck")
  12.  
    E Failed: bad luck
  13.  
     
  14.  
    test_50.py:6: Failed
  15.  
    _______________________________ test_num[25] _______________________________
  16.  
     
  17.  
    i = 25
  18.  
     
  19.  
    @pytest.mark.parametrize("i",range(50))
  20.  
    def test_num(i):
  21.  
    if i in (17,25):
  22.  
    > pytest.fail("bad luck")
  23.  
    E Failed: bad luck
  24.  
     
  25.  
    test_50.py:6: Failed
  26.  
    2 failed,48 passed in 0.12 seconds

如果你然后运行它--lf:

  1.  
    Copy$ pytest --lf
  2.  
    =========================== test session starts ============================
  3.  
    platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
  4.  
    cachedir: $PYTHON_PREFIX/.pytest_cache
  5.  
    rootdir: $REGENDOC_TMPDIR
  6.  
    collected 50 items / 48 deselected / 2 selected
  7.  
    run-last-failure: rerun previous 2 failures
  8.  
     
  9.  
    test_50.py FF [100%]
  10.  
     
  11.  
    ================================= FAILURES =================================
  12.  
    _______________________________ test_num[17] _______________________________
  13.  
     
  14.  
    i = 17
  15.  
     
  16.  
    @pytest.mark.parametrize("i",range(50))
  17.  
    def test_num(i):
  18.  
    if i in (17,25):
  19.  
    > pytest.fail("bad luck")
  20.  
    E Failed: bad luck
  21.  
     
  22.  
    test_50.py:6: Failed
  23.  
    _______________________________ test_num[25] _______________________________
  24.  
     
  25.  
    i = 25
  26.  
     
  27.  
    @pytest.mark.parametrize("i",range(50))
  28.  
    def test_num(i):
  29.  
    if i in (17,25):
  30.  
    > pytest.fail("bad luck")
  31.  
    E Failed: bad luck
  32.  
     
  33.  
    test_50.py:6: Failed
  34.  
    ================= 2 failed,48 deselected in 0.12 seconds ==================

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

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

  1.  
    Copy$ pytest --ff
  2.  
    =========================== test session starts ============================
  3.  
    platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
  4.  
    cachedir: $PYTHON_PREFIX/.pytest_cache
  5.  
    rootdir: $REGENDOC_TMPDIR
  6.  
    collected 50 items
  7.  
    run-last-failure: rerun previous 2 failures first
  8.  
     
  9.  
    test_50.py FF................................................ [100%]
  10.  
     
  11.  
    ================================= FAILURES =================================
  12.  
    _______________________________ test_num[17] _______________________________
  13.  
     
  14.  
    i = 17
  15.  
     
  16.  
    @pytest.mark.parametrize("i",range(50))
  17.  
    def test_num(i):
  18.  
    if i in (17,25):
  19.  
    > pytest.fail("bad luck")
  20.  
    E Failed: bad luck
  21.  
     
  22.  
    test_50.py:6: Failed
  23.  
    _______________________________ test_num[25] _______________________________
  24.  
     
  25.  
    i = 25
  26.  
     
  27.  
    @pytest.mark.parametrize("i",range(50))
  28.  
    def test_num(i):
  29.  
    if i in (17,25):
  30.  
    > pytest.fail("bad luck")
  31.  
    E Failed: bad luck
  32.  
     
  33.  
    test_50.py:6: Failed
  34.  
    =================== 2 failed,48 passed in 0.12 seconds ====================

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

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

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

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

新的config.cache对象

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

  1.  
    Copy# content of test_caching.pyimport pytest
  2.  
    import time
  3.  
     
  4.  
    defexpensive_computation():
  5.  
    print("running expensive computation...")
  6.  
     
  7.  
    @pytest.fixturedefmydata(request):
  8.  
    val = request.config.cache.get("example/value",None)
  9.  
    if val isNone:
  10.  
    expensive_computation()
  11.  
    val = 42
  12.  
    request.config.cache.set("example/value",val)
  13.  
    return val
  14.  
     
  15.  
    deftest_function(mydata):
  16.  
    assert mydata == 23

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

  1.  
    Copy$ pytest -q
  2.  
    F [100%]
  3.  
    ================================= FAILURES =================================
  4.  
    ______________________________ test_function _______________________________
  5.  
     
  6.  
    mydata = 42
  7.  
     
  8.  
    def test_function(mydata):
  9.  
    > assert mydata == 23
  10.  
    E assert 42 == 23
  11.  
     
  12.  
    test_caching.py:17: AssertionError
  13.  
    -------------------------- Captured stdout setup ---------------------------
  14.  
    running expensive computation...
  15.  
    1 failed in 0.12 seconds

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

  1.  
    Copy$ pytest -q
  2.  
    F [100%]
  3.  
    ================================= FAILURES =================================
  4.  
    ______________________________ test_function _______________________________
  5.  
     
  6.  
    mydata = 42
  7.  
     
  8.  
    def test_function(mydata):
  9.  
    > assert mydata == 23
  10.  
    E assert 42 == 23
  11.  
     
  12.  
    test_caching.py:17: AssertionError
  13.  
    1 failed in 0.12 seconds

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

检查缓存内容

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

  1.  
    Copy$ pytest --cache-show
  2.  
    =========================== test session starts ============================
  3.  
    platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
  4.  
    cachedir: $PYTHON_PREFIX/.pytest_cache
  5.  
    rootdir: $REGENDOC_TMPDIR
  6.  
    cachedir: $PYTHON_PREFIX/.pytest_cache
  7.  
    ------------------------------- cache values -------------------------------
  8.  
    cache/lastfailed contains:
  9.  
    {'test_50.py::test_num[17]': True,
  10.  
    'test_50.py::test_num[25]': True,
  11.  
    'test_assert1.py::test_function': True,
  12.  
    'test_assert2.py::test_set_comparison': True,
  13.  
    'test_caching.py::test_function': True,
  14.  
    'test_foocompare.py::test_compare': True}
  15.  
    cache/nodeids contains:
  16.  
    ['test_caching.py::test_function']
  17.  
    cache/stepwise contains:
  18.  
    []
  19.  
    example/value contains:
  20.  
    42
  21.  
     
  22.  
    ======================= no tests ran in 0.12 seconds =======================

清除缓存内容

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

Copypytest --cache-clear

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

逐步修复失败用例

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

unittest.TestCase支持

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

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

Copypytest 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样式测试中引用它的示例如:

  1.  
    Copy# content of conftest.py# we define a fixture function below and it will be "used" by# referencing its name from testsimport pytest
  2.  
     
  3.  
    @pytest.fixture(scope="class")defdb_class(request):
  4.  
    classDummyDB(object):
  5.  
    pass# set a class attribute on the invoking test context
  6.  
    request.cls.db = DummyDB()

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

  1.  
    Copy# content of test_unittest_db.pyimport unittest
  2.  
    import pytest
  3.  
     
  4.  
    @pytest.mark.usefixtures("db_class")classMyTest(unittest.TestCase):
  5.  
    deftest_method1(self):
  6.  
    asserthasattr(self,"db")
  7.  
    assert0,self.db # fail for demo purposesdeftest_method2(self):
  8.  
    assert0,self.db # fail for demo purposes

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

  1.  
    Copy$ pytest test_unittest_db.py
  2.  
    =========================== test session starts ============================
  3.  
    platform linux -- Python 3.x.y,pytest-4.x.y,py-1.x.y,pluggy-0.x.y
  4.  
    cachedir: $PYTHON_PREFIX/.pytest_cache
  5.  
    rootdir: $REGENDOC_TMPDIR
  6.  
    collected 2 items
  7.  
     
  8.  
    test_unittest_db.py FF [100%]
  9.  
     
  10.  
    ================================= FAILURES =================================
  11.  
    ___________________________ MyTest.test_method1 ____________________________
  12.  
     
  13.  
    self = <test_unittest_db.MyTest testMethod=test_method1>
  14.  
     
  15.  
    def test_method1(self):
  16.  
    assert hasattr(self,"db")
  17.  
    > assert 0,self.db # fail for demo purposes
  18.  
    E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
  19.  
    E assert 0
  20.  
     
  21.  
    test_unittest_db.py:9: AssertionError
  22.  
    ___________________________ MyTest.test_method2 ____________________________
  23.  
     
  24.  
    self = <test_unittest_db.MyTest testMethod=test_method2>
  25.  
     
  26.  
    def test_method2(self):
  27.  
    > assert 0,self.db # fail for demo purposes
  28.  
    E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef>
  29.  
    E assert 0
  30.  
     
  31.  
    test_unittest_db.py:12: AssertionError
  32.  
    ========================= 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临时目录:

  1.  
    Copy# content of test_unittest_cleandir.pyimport pytest
  2.  
    import unittest
  3.  
     
  4.  
    classMyTest(unittest.TestCase):
  5.  
     
  6.  
    @pytest.fixture(autouse=True)definitdir(self,tmpdir):
  7.  
    tmpdir.chdir() # change to pytest-provided temporary directory
  8.  
    tmpdir.join("samplefile.ini").write("# testdata")
  9.  
     
  10.  
    deftest_method(self):
  11.  
    withopen("samplefile.ini") as f:
  12.  
    s = f.read()
  13.  
    assert"testdata"in s

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

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

  1.  
    Copy$ pytest -q test_unittest_cleandir.py
  2.  
    . [100%]
  3.  
    1 passed in0.12 seconds

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

注意

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

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

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

注意

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

注意

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

实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

学新通
学新通

如果对你有帮助的话,点个赞收个藏,给作者一个鼓励。也方便你下次能够快速查找。

如有不懂还要咨询下方小卡片,博主也希望和志同道合的测试人员一起学习进步

在适当的年龄,选择适当的岗位,尽量去发挥好自己的优势。

我的自动化测试开发之路,一路走来都离不每个阶段的计划,因为自己喜欢规划和总结,

测试开发视频教程、学习笔记领取传送门!!!

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbagf
系列文章
更多 icon
同类精品
更多 icon
继续加载