Pytest插件pytest-assume多重断言

pytest,插件,assume,多重,断言 · 浏览次数 : 197

小编点评

## Summary of test cases This document summarizes the purpose and functionality of different pytest plugins for asserting multiple conditions: **1. `pytest.assume`:** * This plugin allows defining a single assumption for a set of tests. * It ignores subsequent assumptions and only raises an error if the first assumption fails. * It can be used with a single `assert` statement or multiple `pytest.assume` calls. **2. `pytest.assume(condition)`:** * This plugin allows explicitly asserting a condition before executing subsequent tests. * It allows more control and debugging capabilities compared to `pytest.assume`. * It can be used in conjunction with `assert` statements or `pytest.skip`. **3. `pytest.assume` context manager:** * This plugin allows defining assumptions within a context manager. * It allows defining and executing multiple assumptions within a single block. * It can be used to simplify and organize multiple assert statements. **4. `pytest.assume` with `with` block:** * This plugin allows defining assumptions within a `with` block. * It allows testing specific scenarios and controlling the flow of execution. * It can be used to verify specific conditions and avoid unwanted execution of other tests. **5. `pytest.assume` with multiple `assert` statements:** * This plugin allows defining multiple assumptions within a single `assert` statement. * It is suitable when verifying various conditions within a single unit test. * It can be used with different assertion functions like `assert`, `assert_equal`, and `assert_not`. **6. `pytest.assume` with nested `assert` statements:** * This plugin allows nesting assumptions within other assumptions. * It allows verifying complex conditional scenarios with multiple layers of assumptions. * It can be used to control the execution flow and ensure specific conditions are met. **Note:** * These plugins can be used together within the same test case. * It is recommended to use context managers for defining and executing assumptions. * Always specify the assertion function used with `assert` statements within the `with` block.

正文

Pytest插件pytest-assume多重断言

背景

import pytest


def test_assume1():
    assert 1 == 2
    print('hello')
    assert 2 == 3


if __name__ == '__main__':
    pytest.main(['-sv', __file__])
  • 这样的代码运行的时候并不会打印hello
  • 同样后面的assert 2==3也不会去操作
  • 而实际测试的时候我们经常性的会遇到要去多重断言的情况

安装

pip install pytest-assume

pip install git+https://github.com/astraw38/pytest-assume.git  

介绍

https://pypi.org/project/pytest-assume/ 不要看,啥都没有

https://github.com/astraw38/pytest-assume github上也就讲了几句

  • A pytest plugin that allows multiple failures per test

用法一、assume

import pytest


def test_assume2():
    pytest.assume( 1 == 2 )
    print('hello')
    pytest.assume( 2 == 3)

if __name__ == '__main__':
    pytest.main(['-sv', __file__])
  • 输出
demo_assume.py::test_assume2 hello
FAILED
...

E               demo_assume.py:16: AssumptionFailure
E               >>	pytest.assume( 1 == 2 )
E               AssertionError: assert False
E               
E               demo_assume.py:18: AssumptionFailure
E               >>	pytest.assume( 2 == 3)
E               AssertionError: assert False
  • 可以看到
    • hello 也输出了
    • assume也断言了第二种

用法二、上下文管理器(推荐)

  • 在用法一种隐含了一个小的瑕疵

    import pytest
    
    
    def test_assume3():
        a = 1
        b = 2
        pytest.assume( a == b )
    
    if __name__ == '__main__':
        pytest.main(['-sv', __file__])
    
  • 它的输出是

    E               demo_assume.py:18: AssumptionFailure
    E               >>	pytest.assume( a == b )
    E               AssertionError: assert False
    
  • 你可以看到,变量的真正的值并没有看到,当然你有很多其他的方法来处理,但能看到显然是更利于你便捷的去定位的

  • 这个时候可以用另外一种做法:上下文管理器

    import pytest
    
    
    def test_assume4():
        a = 1
        b = 2
        with pytest.assume: assert a==b
    
    if __name__ == '__main__':
        pytest.main(['-sv', __file__])
    
  • 输出

    E       demo_assume.py:18: AssumptionFailure
    E       >>	with pytest.assume: assert a==b
    E       AssertionError: assert 1 == 2
    
    • 你可以清晰的看到变量的值了
  • 注意,在这种写法中,你要写assert,而第一种写法中你不需要用到assert的


  • 在with的写法中,你可以在一个块中assert多个断言内容,但这样是不推荐的

    import pytest
    
    
    def test_assume5():
        a = 1
        b = 2
        with pytest.assume:
            assert a==b
            assert 1==2
            assert 3==3
    
    if __name__ == '__main__':
        pytest.main(['-sv', __file__])
    
  • 输出:最终你就看到了第二个错误的信息,a==b的断言被你忽略掉了

    E           demo_assume.py:21: AssumptionFailure
    E           >>	assert 3==3
    E           AssertionError: assert 1 == 2
    
  • 你应该这样写

    import pytest
    
    
    def test_assume6():
        a = 1
        b = 2
        with pytest.assume:     assert a == b
        with pytest.assume:     assert 1 == 2
        with pytest.assume:     assert 3 == 3
    
    
    if __name__ == '__main__':
        pytest.main(['-sv', __file__])
    
  • 输出

           with pytest.assume:     assert a == b
    >       with pytest.assume:     assert 1 == 2
    E       pytest_assume.plugin.FailedAssumption: 
    E       2 Failed Assumptions:
    E       
    E       demo_assume.py:18: AssumptionFailure
    E       >>	with pytest.assume:     assert a == b
    E       AssertionError: assert 1 == 2
    E       
    E       demo_assume.py:19: AssumptionFailure
    E       >>	with pytest.assume:     assert 1 == 2
    E       AssertionError: assert 1 == 2
    

与Pytest插件pytest-assume多重断言相似的内容:

Pytest插件pytest-assume多重断言

Pytest插件pytest-assume多重断言 背景 import pytest def test_assume1(): assert 1 == 2 print('hello') assert 2 == 3 if __name__ == '__main__': pytest.main(['-sv

Pytest插件pytest-order指定用例顺序

Pytest插件pytest-order指定用例顺序 安装 pip install pytest-order 注意不是pytest-ordering 说起来这里有个故事 关于pytest-ordering和pytest-order https://github.com/ftobia/pytest-o

Pytest插件pytest-repeat重复执行

Pytest插件pytest-repeat重复执行 安装 pip install pytest-repeat doc https://pypi.org/project/pytest-repeat/ https://github.com/pytest-dev/pytest-repeat 2020年10

Pytest插件pytest-rerunfailures失败重跑

Pytest插件pytest-rerunfailures失败重跑 安装 pip install pytest-rerunfailures doc https://github.com/pytest-dev/pytest-rerunfailures https://pypi.org/project/p

Python中的枚举类enum

0. 本文来历 上一篇文章,我写了Pytest插件pytest-order指定用例顺序 我当时就比较好奇它的顺序和英文的对应关系,肯定是写死的,找了下就发现在源码sorter.py中定义了一个dict如下 orders_map = { "first": 0, "second": 1, "third"

浅谈Pytest中的marker

浅谈Pytest中的marker 没有注册marker 我们写一个简单的测试 # test_demo.py import pytest @pytest.mark.login def test_demo(): assert True 你运行的话会有如下提示 test_demo.py:4: Pytest

浅谈Pytest中的warning处理

浅谈Pytest中的warning处理 没有处理warning 我们写一个简单的测试 import pytest def test_demo(): import warnings warnings.warn('test warn',DeprecationWarning) assert True if

详谈pytest中的xfail

详谈pytest中的xfail 原文链接: https://docs.pytest.org/en/7.2.x/how-to/skipping.html 链接中详细阐述了skip和xfail两种情况 xfail 应该译作expected fail,预期失败(我知道这个用例是因为某些原因会失败的) 有哪

数据驱动测试-从方法探研到最佳实践

作者:刘红妍 导读 在自动化测试实践中,测试数据是制造测试场景的必要条件,本文主要讲述了在沟通自动化框架如何分层,数据如何存储,以及基于单元测试pytest下如何执行。并通过实践案例分享,提供数据驱动测试的具体落地方案。 基本概念 数据驱动测试(DDT)是一种方法,其中在数据源的帮助下重复执行相同顺

pytest7.4版本的一个变更,可能会影响你的项目

# pytest7.4版本的一个变更,可能会影响你的项目 > 本文撰写于 2023.7.10 # 准备工作 - 项目结构如下 ``` D:\Gitee\DemoRepo (17.97MB) +-- testCases (1.03KB) | +-- conftest.py (252b) | +-- p