docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    Inheritance
    object
    Constraint
    AllocatingGCMemoryConstraint
    Implements
    IConstraint
    IResolveConstraint
    Inherited Members
    Constraint.ApplyTo<TActual>(ref TActual)
    Constraint.GetTestObject<TActual>(ActualValueDelegate<TActual>)
    Constraint.ToString()
    Constraint.GetStringRepresentation()
    Constraint.After(int)
    Constraint.After(int, int)
    Constraint.DisplayName
    Constraint.Arguments
    Constraint.Builder
    Constraint.And
    Constraint.With
    Constraint.Or
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: UnityEngine.TestTools.Constraints
    Assembly: UnityEngine.TestRunner.dll
    Syntax
    public class AllocatingGCMemoryConstraint : Constraint, IConstraint, IResolveConstraint
    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

    Description

    The Description of what this constraint tests, for to use in messages and in the ConstraintResult.

    Declaration
    public override string Description { get; }
    Property Value
    Type Description
    string
    Overrides
    NUnit.Framework.Constraints.Constraint.Description

    Methods

    ApplyTo(object)

    Applies GC memory constraint to the test.

    Declaration
    public override ConstraintResult ApplyTo(object obj)
    Parameters
    Type Name Description
    object obj

    An object to apply the GC constraint to. Should be a NUnit.Framework.TestDelegate.

    Returns
    Type Description
    ConstraintResult

    A ConstraintResult

    Overrides
    Constraint.ApplyTo(object)
    Exceptions
    Type Condition
    ArgumentNullException

    Throws a ArgumentNullException if the provided object is null.

    ArgumentException

    Throws a ArgumentException if the provided object is not a NUnit.Framework.TestDelegate.

    ApplyTo<TActual>(ActualValueDelegate<TActual>)

    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.

    Declaration
    public override ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
    Parameters
    Type Name Description
    ActualValueDelegate<TActual> del

    A reference to the value delegate to be tested

    Returns
    Type Description
    ConstraintResult

    A ConstraintResult

    Type Parameters
    Name Description
    TActual

    The type of the actual value delegate to be tested.

    Overrides
    NUnit.Framework.Constraints.Constraint.ApplyTo<TActual>(NUnit.Framework.Constraints.ActualValueDelegate<TActual>)
    Exceptions
    Type Condition
    ArgumentNullException

    Throws a ArgumentNullException if the provided delegate is null.

    Implements

    NUnit.Framework.Constraints.IConstraint
    NUnit.Framework.Constraints.IResolveConstraint
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)