我有这两个值:
Expected Value (fResult)= 103393.431493782901937514
Actual Value (output) = 103393.431493782901937514
当我断言该值时,我的预期结果被认为是 103393.431493783m
因此,我的断言失败了。任何人都可以在这方面提供帮助。
Assert.That(output, Is.EqualTo(fResult));
更多信息:实际值和预期值都是 Decimal 数据类型,
[TestCase("value1", "value2", 5, 103393.431493782901937514)]
public void converFormulaforPressure(String Fromunit, String toUnit, decimal Avalue, decimal fResult) {
var output = ut1.Convert(ut1.GetUnit("Pressure", Fromunit), ut1.GetUnit("Pressure", toUnit), Avalue).Val;
Assert.That(output, Is.EqualTo(fResult));
}
ut1.Convert 是一种转换值并给出实际结果的方法。
最佳答案
您需要提供容差级别并且您不能将decimal
作为TestCase
参数传递。您提供的代码将最后一个值作为 double
传递,因此在您运行断言时会进行舍入。您可以改用 TestCaseSource
来解决这个问题。
下面的测试通过了:
private static readonly object[] TestCases = {
new object[] {"value1", "value2", 5m, 103393.431493782901937514m}
};
[Test, TestCaseSource("TestCases")]
public void TestExample(string fromUnit, string toUnit, decimal value, decimal fResult) {
//Replace the line below with your convert method using values from testCase
var output = 103393.431493782901937514m;
Assert.That(output, Is.EqualTo(fResult).Within(0.00000000000001));
}
关于nunit - 即使十进制数字正确,断言也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31856809/