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:
-burst-disable-compilation
to the command line.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:
CompilationPipeline.codeOptimization = CodeOptimization.Debug
.-debugCodeOptimization
to the command line.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.
To ignore tests when running with Code Coverage, use the ConditionalIgnore attribute, passing the "IgnoreForCoverage"
ID.
public class MyTestClass
{
[Test, ConditionalIgnore("IgnoreForCoverage", "This test is disabled when ran with code coverage")]
public void TestNeverRunningWithCodeCoverage()
{
Assert.Pass();
}
}