docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Run code before a test

    Add setup logic that runs before the graphics test build process. For example, you can use pre-build steps to validate scenes, configure project settings, or prepare assets.

    Bake lighting before a test

    To bake lighting before a test, add the [TestFixture] and [BakeLighting] attribute to your test class with the paths of the scenes to bake:

    For example:

    [TestFixture]
    [BakeLighting("Assets/Scenes/TestScene.unity", order: 1)]
    public class LightingTests
    {
        // Tests that require baked lighting
    }
    

    The order parameter determines when the lighting bake occurs relative to other pre-build steps. Unity executes tests with attributes in ascending order. You can use negative numbers.

    Add your own code before a test

    To add your own code before a test run, create a custom attribute that extends GraphicsPrebuildSetupAttribute. The code runs before Unity collects the reference images and registers AssetBundles.

    Follow these steps:

    1. Create a class that extends GraphicsPrebuildSetupAttribute.

    2. Implement the Setup method with your code.

    3. In your test class, add the name of your attribute as an attribute above the class declaration, along with [TestFixture].

      For example:

      [TestFixture]
      [MyAttribute(order: 0)]
      

      The order attribute determines the order. You can use negative numbers. You can also set the order in the constructor, for example public ValidateScenesAttribute(int order = 0) : base(order) { }

    Note: If you use Unity Editor APIs in the code, make sure to wrap the code in a #if UNITY_EDITOR macro to ensure the build compiles successfully.

    Example

    The following example creates an attribute that validates scenes exist.

    public class ValidateScenesAttribute : GraphicsPrebuildSetupAttribute
    {
        public ValidateScenesAttribute(int order = 0) : base(order) { }
    
        public override void Setup()
        {
            EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;
            foreach (EditorBuildSettingsScene scene in scenes)
            {
                if (!File.Exists(scene.path))
                    Debug.LogError($"Scene missing: {scene.path}");
            }
        }
    }
    

    To use the attribute from the example, add the following above a test method:

    [TestFixture]
    [ValidateScenes(order: 0)]
    

    Additional resources

    • Customizing and optimizing tests
    • Write a scene test
    In This Article
    Back to top
    Copyright © 2026 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)