This exercise will cover the custom equality comparers included in Unity Test Framework, such as Vector3EqualityComparer
. These are used to assert on e.g. Vectors.
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 Vector3
objects.
An 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 Vector3EqualityComparer
, instead of using the default in .instance. For example you can up the tolerance to 0.01f with the following:
Assert.That(actual, Is.EqualTo(expected).Using(new Vector3EqualityComparer(0.01f));
For a list of all available custom comparers, see Custom equality comparers.
Similar to the project for exercise 3, the sample 4_CustomComparison
contains a ValueOutputter
class.
Verify that the ValueOutputter
returns the correct values from its methods:
GetVector3()
should return a Vector3
that is roughly equal to (10.333, 3, 9.666).
GetFloat()
should return a float
that is roughly 19.333. This is the same as previous exercise, but you can try to solve this with a FloatEqualityComparer
.
GetQuaternion
should return a QuaternionUnity’s standard way of representing rotations as data. When writing code that deals with rotations, you should usually use the Quaternion class and its methods. More info
See in Glossary object that should be roughly equal to (10f, 0f, 7.33333f, 0f).
ToString
on Vector3
rounds the value off before displaying it, the two values in the string message might be equal, even when their Vector3
values are not.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)));
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.