pytest 测试框架学习(3):pytest.approx

2023-10-12 00:59

本文主要是介绍pytest 测试框架学习(3):pytest.approx,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pytest.approx

  • 前言
  • 含义
  • 使用
    • 单数
    • 元祖
    • 字典
    • numpy 数组:
    • 相对误差 和 绝对误差
  • 进阶

前言

经过 API 我们已经了解到 pytest 中包括的 API 大致内容,接下来我们详细看看 Functions 中的 pytest.approx

含义

approx:在一定误差范围内断言两个数字(或两组数字)相等。
源码如图:
在这里插入图片描述

使用

我们知道计算机运算浮点数的复杂性,我们直观认为相等的而实际上并不相等;

单数

0.1 + 0.2 == 0.3
# 我们认为上方计算出来结果进行比对返回应该是 True,而实际返回
False

而 approx 可以帮我们解决这个问题:

from pytest import approx
0.1 + 0.2 == approx(0.3)

元祖

(0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6))

字典

{'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6})

numpy 数组:

numpy 介绍和使用

import numpy as np
np.array([0.1, 0.2]) + np.array([0.2, 0.4]) == approx(np.array([0.3, 0.6]))

如果 numpy 数组中结果一致,则可以直接填入一个标量,如:

import numpy as np
np.array([0.1, 0.2]) + np.array([0.2, 0.1]) == approx(0.3)

相对误差 和 绝对误差

默认情况,approx() 使用的为 相对误差,误差的范围是 1e-6 也就是百万分之一。

  1. 指定相对误差:
    在这里插入图片描述
  2. 只指定绝对误差,不指定相对误差,则根据绝对误差来进行比较:
    在这里插入图片描述
  3. 指定两个,只要满足其中一个,则认为是相等的:
    在这里插入图片描述

进阶

主要包括:

  1. 其他方法的浮点数比较与 pytest.approx() 的区别:
    技术能力有限,这里不做细致说明,贴出官网源码。
math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)
"""
- ``math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)``:  True if the relativetolerance is met w.r.t. either ``a`` or ``b`` or if the absolutetolerance is met.  Because the relative tolerance is calculated w.r.t.both ``a`` and ``b``, this test is symmetric (i.e.  neither ``a`` nor``b`` is a "reference value").  You have to specify an absolute toleranceif you want to compare to ``0.0`` because there is no tolerance bydefault.  Only available in python>=3.5.  `More information...`____ https://docs.python.org/3/library/math.html#math.isclose
"""numpy.isclose(a, b, rtol=1e-5, atol=1e-8)
"""
- ``numpy.isclose(a, b, rtol=1e-5, atol=1e-8)``: True if the differencebetween ``a`` and ``b`` is less that the sum of the relative tolerancew.r.t. ``b`` and the absolute tolerance.  Because the relative toleranceis only calculated w.r.t. ``b``, this test is asymmetric and you canthink of ``b`` as the reference value.  Support for comparing sequencesis provided by ``numpy.allclose``.  `More information...`____ http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.isclose.html
"""unittest.TestCase.assertAlmostEqual(a, b)
"""
- ``unittest.TestCase.assertAlmostEqual(a, b)``: True if ``a`` and ``b``are within an absolute tolerance of ``1e-7``.  No relative tolerance isconsidered and the absolute tolerance cannot be changed, so this functionis not appropriate for very large or very small numbers.  Also, it's onlyavailable in subclasses of ``unittest.TestCase`` and it's ugly because itdoesn't follow PEP8.  `More information...`____ https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual
"""a == pytest.approx(b, rel=1e-6, abs=1e-12)
"""
- ``a == pytest.approx(b, rel=1e-6, abs=1e-12)``: True if the relativetolerance is met w.r.t. ``b`` or if the absolute tolerance is met.Because the relative tolerance is only calculated w.r.t. ``b``, this testis asymmetric and you can think of ``b`` as the reference value.  In thespecial case that you explicitly specify an absolute tolerance but not arelative tolerance, only the absolute tolerance is considered.
"""
  1. 运算符比较
"""
In order to avoid inconsistent behavior, ``TypeError`` israised for ``>``, ``>=``, ``<`` and ``<=`` comparisons.The example below illustrates the problem::assert approx(0.1) > 0.1 + 1e-10  # calls approx(0.1).__gt__(0.1 + 1e-10)assert 0.1 + 1e-10 > approx(0.1)  # calls approx(0.1).__lt__(0.1 + 1e-10)In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)``to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used tocomparison. This is because the call hierarchy of rich comparisonsfollows a fixed behavior. `More information...`____ https://docs.python.org/3/reference/datamodel.html#object.__ge__
"""

说明:本篇参考官网并加入自己些许理解翻译而来,觉得有用,可以点赞和赞赏哦(^ v ^),谢谢支持;如果有不足地方,可留言评论。后续将继续更新。
在这里插入图片描述

这篇关于pytest 测试框架学习(3):pytest.approx的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/192104

相关文章

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

python多线程并发测试过程

《python多线程并发测试过程》:本文主要介绍python多线程并发测试过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、并发与并行?二、同步与异步的概念?三、线程与进程的区别?需求1:多线程执行不同任务需求2:多线程执行相同任务总结一、并发与并行?1、

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过