c# - xUnit 断言两个值相等且具有一定的容差
问题描述
我正在尝试将两个数字的精度与一些公差进行比较。
这是在 nUnit 中检查它的方式:
Assert.That(turnOver, Is.EqualTo(turnoverExpected).Within(0.00001).Percent);
我试图在 xUnit 中做同样的事情,但这就是我想出的全部:
double tolerance = 0.00001;
Assert.Equal(turnOver, turnoverExpected, tolerance);
这不会编译,因为Assert.Equal
不采用 type 的第三个参数double
。
任何人都知道在 xUnit 中执行此操作的好方法吗?
解决方案
您可能稍微误解了Assert.Equal(expected, actual, precision)
方法中的最后一个参数(精度)。
/// <param name="precision">The number of decimal places (valid values: 0-15)</param>
因此,例如,如果您想比较0.00021
并且您只想比较0.00022
4 位小数,您可以这样做(它将返回true
):
Assert.Equal(0.00021, 0.00022, 4); // true
这将返回false
:
Assert.Equal(0.00021, 0.00022, 5); // false
推荐阅读
- r - 从列表或矩阵中按顺序替换数据框中的 NA 值
- ios - 当您将 self 分配给委托时,究竟会发生什么?
- reactjs - 如何在 react-bootstrap 应用程序中创建首次访问弹出窗口
- reactjs - ReactJS 重定向与链接中的 id
- spring - setApplicationContext(ApplicationContext applicationContext) 从未调用过
- unity3d - 如何在 Unity Android 中打开 PlayStore 页面
- java - 是否有推荐的策略来为多个表中的多个模型保留一个时间戳?
- java - 如何将对象传递给休息模板?
- autohotkey - 通过鼠标点击在 AHK 中构建一个数组
- firebase - 你能得到以 .JPG/.PNG 结尾的 Firebase FireStorage 图像 URL,没有 alt 或 token 参数吗?