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:
Create an Editor-only test assembly that references
UnityEditor.TestTools.Graphics.Add the shader you want to test to the
Assetsfolder of the Project window.Create a
ShaderTestFrameworkinstance. The recommended approach is to create and dispose of it in[OneTimeSetUp]and[OneTimeTearDown]methods. Refer to the HLSL section for an example.Create a C# method with a
[Test]attribute.In the method, create a
ShaderlabShaderParamsobject. You don't need to pass in any parameters.ShaderlabShaderParams shaderParams = new ShaderlabShaderParams();Get a handle to the shader by calling
shaderTestFramework.LoadShaderwith the internal name of the shader and the parameters.ShaderHandle shader = shaderTestFramework.LoadShader("SampleTest/ShaderlabShader.shader", shaderParams);Call
ExecuteShaderwith the shader handle.ShaderlabShaderData result = shaderTestFramework.ExecuteShader<ShaderlabShaderData>(shader);Use the
VertexandFragmentproperties of the returnedShaderlabShaderDatain yourAssertstatement. For example:Assert.That(result.Fragment, Is.Not.Null);Vertexis a Vector3 array of the vertex output positions.Fragmentis 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:
Create an Editor-only test assembly that references
UnityEditor.TestTools.Graphics.Add the HLSL file you want to test to the
Assetsfolder of the Project window.Create a
ShaderTestFrameworkinstance.ShaderTestFramework shaderTestFramework;Create a test method with a
[Test]attribute. Inside the method, create anHlslShaderParamsobject 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
VectororMatrix. For example, useVector3for an HLSLfloat3orint4, orMatrix4x4for an HLSLfloat4x4.Call
LoadShaderwith the path to your.hlslfile and the shader parameters.ShaderHandle shader = shaderTestFramework.LoadShader("Assets/Shaders/SampleTestShader.hlsl", shaderParams);Call
ExecuteShader<T>.Tmust match the return type of the shader.float result = shaderTestFramework.ExecuteShader<float>(shader);For vector return types, use
VectororMatrix. For example, useVector3for an HLSLfloat3orint4, orMatrix4x4for an HLSLfloat4x4.Check the result with an
Assertstatement. 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
OneTimeSetupmethod to initialize variables or state that you share across tests, such as aShaderTestFrameworkinstance. - Create a
OneTimeTearDownmethod 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.