docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Write a shader test

    To test shaders without rendering a full scene, create tests that check ShaderLab shader objects or HLSL shader functions.

    Note: You can't test shader graphs, compute shaders, CG shaders, or ray tracing shaders.

    For more information about using a test after you write it, refer to Run a graphics test for the first time and Run a test and check the results.

    Write a ShaderLab test

    To write a ShaderLab test, follow these steps:

    1. Create an Editor-only test assembly that references UnityEditor.TestTools.Graphics.

    2. Add the shader you want to test to the Assets folder of the Project window.

    3. Create a ShaderTestFramework instance. The recommended approach is to create and dispose of it in [OneTimeSetUp] and [OneTimeTearDown] methods. Refer to the HLSL section for an example.

    4. Create a C# method with a [Test] attribute.

    5. In the method, create a ShaderlabShaderParams object. You don't need to pass in any parameters.

      ShaderlabShaderParams shaderParams = new ShaderlabShaderParams();
      
    6. Get a handle to the shader by calling shaderTestFramework.LoadShader with the internal name of the shader and the parameters.

      ShaderHandle shader = shaderTestFramework.LoadShader("SampleTest/ShaderlabShader.shader", shaderParams);
      
    7. Call ExecuteShader with the shader handle.

      ShaderlabShaderData result = shaderTestFramework.ExecuteShader<ShaderlabShaderData>(shader);
      
    8. Use the Vertex and Fragment properties of the returned ShaderlabShaderData in your Assert statement. For example:

      Assert.That(result.Fragment, Is.Not.Null);
      

      Vertex is a Vector3 array of the vertex output positions. Fragment is the fragment output as a texture.

    Write an HLSL method test

    Unity automatically generates wrapper shaders for HLSL methods, so you can test the method without a surrounding ShaderLab shader.

    To write an HLSL method test, follow these steps:

    1. Create an Editor-only test assembly that references UnityEditor.TestTools.Graphics.

    2. Add the HLSL file you want to test to the Assets folder of the Project window.

    3. Create a ShaderTestFramework instance.

      ShaderTestFramework shaderTestFramework;
      
    4. Create a test method with a [Test] attribute. Inside the method, create an HlslShaderParams object with the HLSL function you want to call and the expected C# return type. For example:

      [Test]
      public void FloatFunction_ReturnsExpected()
      {
          HlslShaderParams shaderParams = new HlslShaderParams("SampleFloatFunction()", typeof(float));
      }
      

      For vector return types, use Vector or Matrix. For example, use Vector3 for an HLSL float3 or int4, or Matrix4x4 for an HLSL float4x4.

    5. Call LoadShader with the path to your .hlsl file and the shader parameters.

      ShaderHandle shader = shaderTestFramework.LoadShader("Assets/Shaders/SampleTestShader.hlsl", shaderParams);
      
    6. Call ExecuteShader<T>. T must match the return type of the shader.

      float result = shaderTestFramework.ExecuteShader<float>(shader);
      

      For vector return types, use Vector or Matrix. For example, use Vector3 for an HLSL float3 or int4, or Matrix4x4 for an HLSL float4x4.

    7. Check the result with an Assert statement. For example:

      Assert.That(result, Is.EqualTo(1.0f));
      

    Run code before or after the test

    The recommended best practice is to do the following:

    • Create a OneTimeSetup method to initialize variables or state that you share across tests, such as a ShaderTestFramework instance.
    • Create a OneTimeTearDown method to clean up any shared resources.

    For example:

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        shaderTestFramework = new ShaderTestFramework();
    }
    
    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        shaderTestFramework.Dispose();
    }
    

    For more information, refer to Setting up and tearing down tests.

    Add parameters to shader tests

    To parameterize a shader test, use the [TestCase] attribute with literal parameters in the function call string. For example:

    [Test]
    [TestCase("SampleIntAdd(1, 2)", 3)]
    [TestCase("SampleIntAdd(0, 0)", 0)]
    [TestCase("SampleIntAdd(-1, 1)", 0)]
    public void IntAdd_ReturnsExpected(string functionCall, int expected)
    {
        ...
    }
    

    For more information, refer to Test cases.

    Additional resources

    • Graphics test workflow
    • Write a scene test
    • Graphics Tests window reference
    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)