Unity Test Framework extends NUnit with Unity-specific custom APIs for comparing, asserting, and providing constraints for your assertions.
A test fails if Unity logs a message other than a regular log or warning message. You can use LogAssert to check for an expected message in the log so the test doesn’t fail when Unity logs the message.
Use LogAssert.Expect before running the code under test, as the check for expected logs runs at the end of each frame.
A test also reports a failure if an expected message does not appear, or if Unity does not log any regular log or warning messages.
The following example uses LogAssert.Expect to expect both a regular log message and an error message in the Editor log:
[Test]
public void LogAssertExample()
{
// Expect a regular log message
LogAssert.Expect(LogType.Log, "Log message");
// The test fails without the following expected log message
Debug.Log("Log message");
// An error log
Debug.LogError("Error message");
// Without expecting an error log, the test would fail
LogAssert.Expect(LogType.Error, "Error message");
}
NUnit allows you to write human-readable, descriptive assertions with the Assert.That mechanism, where the first parameter is an object under test and the second parameter describes conditions that the object has to meet.
Unity Test Framework extends this with a custom constraint type and declared an overlay Is class. To resolve ambiguity between the original implementation and the custom one you must explicitly declare it with a using statement or via addressing through the full type name UnityEngine.TestTools.Constraints.Is.
Unity Test Framework provides custom equality comparers and utilities to enable easier verification of custom Unity type values in your tests. Comparable types include [Color], [Quaternion] and Vectors.
Use these classes to compare two objects of the same type for equality within the range of a given tolerance using NUnit constraints or Unity constraints.
Call Instance to apply the default calculation error value to the comparison. To set a specific error value, instantiate a new comparer object using a one argument constructor ctor(float error).
For the full list of available comparers and example usage, refer to the API reference for UnityEngine.TestTools.Utils.
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.