docs.unity3d.com
    Show / Hide Table of Contents

    Using Code Coverage

    Using Code Coverage with Burst compiler

    If you use the Burst package and have jobs compiled with Burst, you will need to disable Burst compilation in order to get full coverage. To disable Burst compilation you can do one of the following:

    • Uncheck Enable Compilation under Jobs > Burst > Enable Compilation.
    • Pass -burst-disable-compilation to the command line.

    Using Code Coverage with Code Optimization

    Code Optimization was introduced in 2020.1. Code Optimization mode defines whether Unity Editor compiles scripts in Debug or Release mode. Debug mode enables C# debugging and it is required in order to obtain accurate code coverage. To ensure Code optimization is set to Debug mode you can do one of the following:

    • Switch to Debug mode in the Editor (bottom right corner, select the Bug icon > Switch to debug mode).
    • Using the CompilationPipeline api, set CompilationPipeline.codeOptimization = CodeOptimization.Debug.
    • Pass -debugCodeOptimization to the command line.

    Excluding code from Code Coverage

    Any code that should not be contributing to the Code Coverage calculation can be excluded by adding the ExcludeFromCoverage attribute. This attribute can be added to Assemblies, Classes, Constructors, Methods and Structs. Note that you can also use the .NET ExcludeFromCodeCoverage attribute.

    Ignoring tests for Code Coverage

    To ignore tests when running with Code Coverage, use the ConditionalIgnore attribute, passing the "IgnoreForCoverage" ID.

    Example

    public class MyTestClass
    {
        [Test, ConditionalIgnore("IgnoreForCoverage", "This test is disabled when ran with code coverage")]
        public void TestNeverRunningWithCodeCoverage()
        {
            Assert.Pass();
        }
    }
    
    In This Article
    • Using Code Coverage with Burst compiler
    • Using Code Coverage with Code Optimization
    • Excluding code from Code Coverage
    • Ignoring tests for Code Coverage
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023