如何在保持默认相等性检查的同时向 NUnit 添加自定义相等性
How to add custom equality to NUnit while also keeping the default equality check
有没有办法制作自定义约束,以便先完成 NUnit 的默认相等性测试,然后再进行自定义相等性测试?
例如,假设我想测试 System.Double
值是否相等,但将 Double.PositiveInfinity
视为等于 Double.NegativeInfinity
。我可以在下面的代码中编写一个简单的测试 Comparison
来检查这种特殊情况,然后只使用 ==
.
如果我用Is.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
然后 NaNisEqualToNaN
失败,因为我没有 NUnit 的默认相等性测试。
如果我使用Is.EqualTo(expected).Or.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
组合自定义测试和默认测试
然后像 ShouldFail_NegativeInfinityIsKindaEqualTo7
这样的失败测试会给出重复的错误消息 Expected: 7.0d or 7.0d But was...
完整代码示例:
using System;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace UnitTest
{
[TestFixture]
public class Class1
{
private Constraint IsKindaEqualTo(double expected)
{
// With this line, ShouldFail_NegativeInfinityIsKindaEqualTo7 reports "Expected: 7.0d or 7.0d But was: ∞"
return Is.EqualTo(expected).Or.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
// With this line, the NaNisEqualToNaN test fails
//return Is.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
}
private static bool Comparison(double x, double y)
{
if (Double.IsInfinity(x) && Double.IsInfinity(y))
{
return true;
}
else
{
return x == y;
}
}
[Test]
public void NegativeInfinityIsKindaEqualToPositiveInfinity()
{
var actual = Double.NegativeInfinity;
var expected = Double.PositiveInfinity;
Assert.That(actual, IsKindaEqualTo(expected));
}
[Test]
public void ShouldFail_NegativeInfinityIsKindaEqualTo7()
{
var actual = Double.PositiveInfinity;
var expected = 7.0;
Assert.That(actual, IsKindaEqualTo(expected));
}
[Test]
public void NaNisEqualToNaN()
{
var actual = Double.NaN;
var expected = Double.NaN;
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void NaNisKindaEqualToNan()
{
var actual = Double.NaN;
var expected = Double.NaN;
Assert.That(actual, IsKindaEqualTo(expected));
}
}
}
NUnit 的相等比较来自一个名为NUnitEqualityComparer
的class。我会在您的 Comparison
方法中创建并使用其中一个,并将所有内容包装在一个比较器中。例如:
private static bool Comparison(double x, double y)
{
var nunitComparer = new NUnitEqualityComparer();
if (nunitComparer.AreEqual(x,y)) return true;
if (Double.IsInfinity(x) && Double.IsInfinity(y))
{
return true;
}
else
{
return x == y;
}
}
有没有办法制作自定义约束,以便先完成 NUnit 的默认相等性测试,然后再进行自定义相等性测试?
例如,假设我想测试 System.Double
值是否相等,但将 Double.PositiveInfinity
视为等于 Double.NegativeInfinity
。我可以在下面的代码中编写一个简单的测试 Comparison
来检查这种特殊情况,然后只使用 ==
.
如果我用
Is.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
然后NaNisEqualToNaN
失败,因为我没有 NUnit 的默认相等性测试。如果我使用
Is.EqualTo(expected).Or.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
组合自定义测试和默认测试 然后像ShouldFail_NegativeInfinityIsKindaEqualTo7
这样的失败测试会给出重复的错误消息Expected: 7.0d or 7.0d But was...
完整代码示例:
using System;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace UnitTest
{
[TestFixture]
public class Class1
{
private Constraint IsKindaEqualTo(double expected)
{
// With this line, ShouldFail_NegativeInfinityIsKindaEqualTo7 reports "Expected: 7.0d or 7.0d But was: ∞"
return Is.EqualTo(expected).Or.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
// With this line, the NaNisEqualToNaN test fails
//return Is.EqualTo(expected).Using<double>((x, y) => Comparison(x, y));
}
private static bool Comparison(double x, double y)
{
if (Double.IsInfinity(x) && Double.IsInfinity(y))
{
return true;
}
else
{
return x == y;
}
}
[Test]
public void NegativeInfinityIsKindaEqualToPositiveInfinity()
{
var actual = Double.NegativeInfinity;
var expected = Double.PositiveInfinity;
Assert.That(actual, IsKindaEqualTo(expected));
}
[Test]
public void ShouldFail_NegativeInfinityIsKindaEqualTo7()
{
var actual = Double.PositiveInfinity;
var expected = 7.0;
Assert.That(actual, IsKindaEqualTo(expected));
}
[Test]
public void NaNisEqualToNaN()
{
var actual = Double.NaN;
var expected = Double.NaN;
Assert.That(actual, Is.EqualTo(expected));
}
[Test]
public void NaNisKindaEqualToNan()
{
var actual = Double.NaN;
var expected = Double.NaN;
Assert.That(actual, IsKindaEqualTo(expected));
}
}
}
NUnit 的相等比较来自一个名为NUnitEqualityComparer
的class。我会在您的 Comparison
方法中创建并使用其中一个,并将所有内容包装在一个比较器中。例如:
private static bool Comparison(double x, double y)
{
var nunitComparer = new NUnitEqualityComparer();
if (nunitComparer.AreEqual(x,y)) return true;
if (Double.IsInfinity(x) && Double.IsInfinity(y))
{
return true;
}
else
{
return x == y;
}
}