4. Custom comparison
Use the Unity Test Framework's custom equality comparers to check for value equality of Unity types.
Read time 2 minutesLast updated 17 days ago
Learning objectives
This exercise will cover the custom equality comparers included in Unity Test Framework, such as . These are used to assert on e.g. Vectors.
Vector3EqualityComparerIntro and motivation
We have extended the assertion capabilities of NUnit with some custom comparisons for Unity-specific objects. A good example of this is the ability to compare two objects.
Vector3An example of its use is:
actual = new Vector3(0.01f, 0.01f, 0f);expected = new Vector3(0.01f, 0.01f, 0f);Assert.That(actual, Is.EqualTo(expected).Using(Vector3EqualityComparer.Instance));
This allows us to verify that the two vectors are identical within a given tolerence. By default the tolerance is 0.0001f. The tolerance can be changed by providing a new , instead of using the default in .instance. For example you can up the tolerance to 0.01f with the following:
Vector3EqualityComparerAssert.That(actual, Is.EqualTo(expected).Using(new Vector3EqualityComparer(0.01f));
For a list of all available custom comparers, see Custom equality comparers.
Exercise
Similar to the project for exercise 3, the sample contains a class.
4_CustomComparisonValueOutputterVerify that the returns the correct values from its methods:
ValueOutputter-
should return a
GetVector3()that is roughly equal to (10.333, 3, 9.666).Vector3 -
should return a
GetFloat()that is roughly 19.333. This is the same as previous exercise, but you can try to solve this with afloat.FloatEqualityComparer -
should return a Quaternion object that should be roughly equal to (10f, 0f, 7.33333f, 0f).
GetQuaternion
Hints
- For some of the exercises, you might need to provide a custom error tolerance to the comparer.
- If the comparison fails, the comparers give a message about the actual and expected value, just like a normal assertion. However, because on
ToStringrounds the value off before displaying it, the two values in the string message might be equal, even when theirVector3values are not.Vector3
Solution
The full solution is available in the sample .
4_CustomComparison_Solution[Test]public void Vector3ReturnsCorrectValue(){ var valueOutputterUnderTest = new ValueOutputter(); var vector3 = valueOutputterUnderTest.GetVector3(); var expected = new Vector3(10.333f, 3f, 9.666f); Assert.That(vector3, Is.EqualTo(expected).Using(new Vector3EqualityComparer(0.001f)));}[Test]public void FloatReturnsCorrectValue(){ var valueOutputterUnderTest = new ValueOutputter(); var actualFloat = valueOutputterUnderTest.GetFloat(); Assert.That(actualFloat, Is.EqualTo(19.333f).Using(new FloatEqualityComparer(0.001f)));}[Test]public void QuaternionReturnsCorrectValue(){ var valueOutputterUnderTest = new ValueOutputter(); var actualValue = valueOutputterUnderTest.GetQuaternion(); var expectedValue = new Quaternion(10f, 0f, 7.33333f, 0f); Assert.That(actualValue, Is.EqualTo(expectedValue).Using(new QuaternionEqualityComparer(0.001f)));}