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
.