在 NUnit 中,如何为结构的属性比较指定容差?
In NUnit how do I specify tolerance for struct's properties comparison?
我有以下 NUnit 测试,由于某种原因结构比较失败:
public struct Record
{
public double P1 { get; set; }
public double P2 { get; set; }
}
[TestFixture]
public class UnitTest
{
[Test]
public void PassingTest()
{
Assert.That(100, Is.EqualTo(99.99999999).Within(.0001));
}
[Test]
public void FailingTest()
{
Assert.That(new Record { P1 = 1.0, P2 = 100 },
Is.EqualTo(new Record { P1 = 1.0, P2 = 99.99999999 }).Within(.0001));
}
[Test]
public void AlsoPassesTest()
{
Assert.That(new Record { P1 = 1.0, P2 = 100 },
Is.EqualTo(new Record { P1 = 1.0, P2 = 100 }).Within(.0001));
}
}
问题(1):为什么structs比较的测试会失败?问题 (2):如果由于未应用公差 "deeply" 而导致结构比较测试失败,我该如何设置才能通过结构比较测试?
您可以为结构的属性做出两个单独的断言:
[Test]
public void FailingTestSoonToPass()
{
var recordToTest = new Record { P1 = 1.0, P2 = 100 };
var recordToCompareWith = new Record { P1 = 1.0, P2 = 99.99999999 };
Assert.That(recordToTest.P1, Is.EqualTo(recordToCompareWith.P1)recordToCompareWith.P1);
Assert.That(recordToTest.P2, Is.EqualTo(recordToCompareWith.P2).Within(.0001));
}
我认为 Tolerance 不适用于整个结构,因为您将它设置为整个结构。
无论如何,根据测试的性质,您可以考虑将此测试拆分为两个单独的测试,尤其是如果只有一个属性必须具有公差而另一个属性需要精确时。
看来你得手写了。像这样:
static class IsAlternatively
{
public static AlternativeEqualConstraint EqualTo(Record r)
{
return new AlternativeEqualConstraint(r);
}
}
class AlternativeEqualConstraint : NUnit.Framework.Constraints.Constraint
{
readonly Record expected;
double tolerance;
public AlternativeEqualConstraint(Record r)
{
this.expected = r;
}
public AlternativeEqualConstraint Within(double tolerance)
{
this.tolerance = tolerance;
return this;
}
public override bool Matches(object obj)
{
actual = obj;
if (!(obj is Record))
return false;
var other = (Record)obj;
return Math.Abs(other.P1 - expected.P1) < tolerance && Math.Abs(other.P2 - expected.P2) < tolerance;
}
public override void WriteDescriptionTo(NUnit.Framework.Constraints.MessageWriter writer)
{
writer.WriteExpectedValue(expected);
writer.WriteMessageLine("Expected within tolerance '{0}'.", tolerance);
}
public override void WriteActualValueTo(NUnit.Framework.Constraints.MessageWriter writer)
{
writer.WriteActualValue(actual);
}
}
当然是这样用:
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 99.99999999, }).Within(.0001)
);
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 100.0, }).Within(.0001)
);
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 66.6, }).Within(.0001)
);
我有以下 NUnit 测试,由于某种原因结构比较失败:
public struct Record
{
public double P1 { get; set; }
public double P2 { get; set; }
}
[TestFixture]
public class UnitTest
{
[Test]
public void PassingTest()
{
Assert.That(100, Is.EqualTo(99.99999999).Within(.0001));
}
[Test]
public void FailingTest()
{
Assert.That(new Record { P1 = 1.0, P2 = 100 },
Is.EqualTo(new Record { P1 = 1.0, P2 = 99.99999999 }).Within(.0001));
}
[Test]
public void AlsoPassesTest()
{
Assert.That(new Record { P1 = 1.0, P2 = 100 },
Is.EqualTo(new Record { P1 = 1.0, P2 = 100 }).Within(.0001));
}
}
问题(1):为什么structs比较的测试会失败?问题 (2):如果由于未应用公差 "deeply" 而导致结构比较测试失败,我该如何设置才能通过结构比较测试?
您可以为结构的属性做出两个单独的断言:
[Test]
public void FailingTestSoonToPass()
{
var recordToTest = new Record { P1 = 1.0, P2 = 100 };
var recordToCompareWith = new Record { P1 = 1.0, P2 = 99.99999999 };
Assert.That(recordToTest.P1, Is.EqualTo(recordToCompareWith.P1)recordToCompareWith.P1);
Assert.That(recordToTest.P2, Is.EqualTo(recordToCompareWith.P2).Within(.0001));
}
我认为 Tolerance 不适用于整个结构,因为您将它设置为整个结构。
无论如何,根据测试的性质,您可以考虑将此测试拆分为两个单独的测试,尤其是如果只有一个属性必须具有公差而另一个属性需要精确时。
看来你得手写了。像这样:
static class IsAlternatively
{
public static AlternativeEqualConstraint EqualTo(Record r)
{
return new AlternativeEqualConstraint(r);
}
}
class AlternativeEqualConstraint : NUnit.Framework.Constraints.Constraint
{
readonly Record expected;
double tolerance;
public AlternativeEqualConstraint(Record r)
{
this.expected = r;
}
public AlternativeEqualConstraint Within(double tolerance)
{
this.tolerance = tolerance;
return this;
}
public override bool Matches(object obj)
{
actual = obj;
if (!(obj is Record))
return false;
var other = (Record)obj;
return Math.Abs(other.P1 - expected.P1) < tolerance && Math.Abs(other.P2 - expected.P2) < tolerance;
}
public override void WriteDescriptionTo(NUnit.Framework.Constraints.MessageWriter writer)
{
writer.WriteExpectedValue(expected);
writer.WriteMessageLine("Expected within tolerance '{0}'.", tolerance);
}
public override void WriteActualValueTo(NUnit.Framework.Constraints.MessageWriter writer)
{
writer.WriteActualValue(actual);
}
}
当然是这样用:
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 99.99999999, }).Within(.0001)
);
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 100.0, }).Within(.0001)
);
Assert.That(new Record { P1 = 1.0, P2 = 100.0, },
IsAlternatively.EqualTo(new Record { P1 = 1.0, P2 = 66.6, }).Within(.0001)
);