Using Code Coverage with Test Runner
When running your tests in the Test Runner you can generate an HTML report which shows which lines of your code the tests cover. This includes both EditMode and PlayMode tests.
If Auto Generate Report is checked then an HTML report will be generated and a file viewer window will open up containing the coverage results and the report. Otherwise, select the Generate from Last button to generate one. The results are based on the assemblies specified in Included Assemblies.
Steps
Make sure Code Coverage is enabled (see Enable Code Coverage).
Open the Code Coverage window (go to Window > General > Code Coverage).
Select the Assembly Definitions you would like to see the coverage for. In this example we selected
Assembly-CSharp
andAssembly-CSharp-Editor
. By default, Unity compiles almost all project scripts into theAssembly-CSharp.dll
managed assembly and all editor scripts into theAssembly-CSharp-Editor.dll
managed assembly.
Switch to the Test Runner and run your EditMode or PlayMode test(s).
Example test:
using NUnit.Framework; using Assert = UnityEngine.Assertions.Assert; public class Tests { [Test] public void MyMonoBehaviour_PublicFunctionCanBeCalled() { MyMonoBehaviour myMonoBehaviour = new MyMonoBehaviour(); Assert.IsTrue(myMonoBehaviour.PublicFunction()); } }
When the test(s) finish running, a file viewer window will open up containing the coverage report.
Note: To generate the report automatically after the Test Runner has finished running the tests you should have Auto Generate Report checked in the Code Coverage window. Alternatively, you can select the Generate from Last button.Select
index.htm
.
This opens the HTML coverage report.
Getting results for EditMode and PlayMode tests
Coverage data is generated from the last set of tests that were run in the Test Runner.
Note: Currently the Test Runner does not support EditMode and PlayMode tests running at the same time. To include coverage for both EditMode and PlayMode tests, these will need to run separately. In this case, the last Coverage Report generated will include the combined coverage of EditMode and PlayMode tests.
If a fresh start is required, select the Clear Data button to clear the Coverage data from all previous test runs for both EditMode and PlayMode tests.