Asserting and comparing
Perform assertions with constraints and compare the values of custom Unity types in your tests.
Read time 2 minutesLast updated 13 days ago
Unity Test Framework extends NUnit with Unity-specific custom APIs for comparing, asserting, and providing constraints for your assertions.
Asserting
A test fails if Unity logs a message other than a regular log or warning message. You can use to check for an expected message in the log so the test doesn't fail when Unity logs the message.
LogAssertUse before running the code under test, as the check for expected logs runs at the end of each frame.
LogAssert.ExpectA 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 to expect both a regular log message and an error message in the Editor log:
LogAssert.Expect[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");}
Asserting with constraints
NUnit allows you to write human-readable, descriptive assertions with the mechanism, where the first parameter is an object under test and the second parameter describes conditions that the object has to meet.
Assert.ThatUnity Test Framework extends this with a custom constraint type and declared an overlay class. To resolve ambiguity between the original implementation and the custom one you must explicitly declare it with a statement or via addressing through the full type name .
IsusingUnityEngine.TestTools.Constraints.IsComparing
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 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 .
Instancector(float error)For the full list of available comparers and example usage, refer to the API reference for .
UnityEngine.TestTools.Utils