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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_38374974/article/details/107154594
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/192104

相关文章

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接