Code Coverage: Spot gaps in your tests
If you’re aiming for test-driven development, you need to make sure that your code is getting properly covered by tests as you go. Originally released with 2019.3, the Code Coverage preview package helps you make sure you haven’t missed anything by providing you a birds-eye visual overview of your code.
When used with the Test Runner, the Code Coverage package shows you which lines of your code are getting tested, in addition to whether the tests have passed or failed. In version 0.3.0-preview with Unity 2020.1 beta, we’ve added several improvements.
The package is a client of the coverage API that we added in Unity 2019.2 and uses a combination of this API and C# reflection to output the test coverage data in the OpenCover format. The package then uses ReportGenerator to parse the OpenCover data and present the coverage data as a series of HTML documents.
To get started with Code Coverage, you need to first install the preview package from the Package Manager. Remember to enable Preview Packages in Edit > Project Settings > Package Manager if you use 2020.1 beta 8 or newer.
Then, Go to Edit > Preferences > General and check Enable Code Coverage. Note that Enabling Code Coverage adds some overhead to the editor and lowers the performance, so it is not recommended to leave it on if you are not performing coverage testing. This will give the package access to the interface for the code coverage data that Mono exposes. Then, restart Unity.
Now you’re ready to start running your tests!
Once a test run has completed, the package will generate an HTML coverage report — which will show the total line coverage percentage and a list of your classes that you can navigate to for a line by line representation of the coverage.
How do you interpret the results? The most important value in your HTML Report will be Line coverage. This shows the proportion of your code that is covered by tests. For example, if it shows 75%, that means that a quarter of all the lines of code that you wrote isn’t covered by testing. The calculation takes into consideration which lines are coverable by tests.
The graph (red line in the screenshot) under Coverage History shows the total percentage coverage for every test run for this project. Aim to keep this percentage as high as possible. If it’s decreasing, consider writing more tests to improve your coverage.
Code Coverage currently supports EditMode and PlayMode tests and allows you to track the code coverage changes over time. It works with the Test Runner. If you don’t have any tests, it offers the Coverage Recording feature which allows capturing coverage data on demand.
Code Coverage workshop
Get started with the help of our step by step workshop! See how you can identify areas of your code that need more testing, even if you haven’t written any automated tests. It takes about 30 minutes to complete. You can find it in Package Manager under Samples, once you have installed the Code Coverage package.
You will find a Worksheet inside the Samples folder.
The workshop guides you through the following steps:
- What is Code Coverage
- Install the Code Coverage package
- Enable Code Coverage
- Understanding the game code: Shoot() function
- Generate a Coverage report from the PlayMode tests
- Add Weapon tests to improve coverage
- Add a test for the LaserController
- Clear the coverage data
- Generate a Coverage report using Coverage Recording
You can also watch the relevant section of our 2020.1 beta webinar to see this feature in action:
For more information see the Code Coverage package documentation.
The Code Coverage package is scheduled to be verified in 2021.1. Feel free to try it out and let us know what you think in this forum thread.