Class AllocatingGCMemoryConstraint
An NUnit test constraint class to test whether a given block of code makes any GC allocations.
Use this class with NUnit's Assert.That() method to make assertions about the GC behaviour of your code. The constraint executes the delegate you provide, and checks if it has caused any GC memory to be allocated. If any GC memory was allocated, the constraint passes; otherwise, the constraint fails.
Usually you negate this constraint to make sure that your delegate does not allocate any GC memory. This is easy to do using the Is class:
Namespace: UnityEngine .TestTools .Constraints
Assembly: solution.dll
Syntax
public class AllocatingGCMemoryConstraint : Constraint
Examples
using NUnit.Framework;
using UnityEngine.TestTools.Constraints;
using Is = UnityEngine.TestTools.Constraints.Is;
public class MyTestClass
{
[Test]
public void SettingAVariableDoesNotAllocate()
{
Assert.That(() => {
int a = 0;
a = 1;
}, Is.Not.AllocatingGCMemory());
}
}
Properties
Name | Description |
---|---|
Description | The Description of what this constraint tests, for to use in messages and in the ConstraintResult. |
Methods
Name | Description |
---|---|
Apply |
Applies GC memory constraint to the test. |
Apply |
Test whether the constraint is satisfied by a given reference. The default implementation simply dereferences the value but derived classes may override it to provide for delayed processing. |