代码覆盖率和测试覆盖率_代码覆盖率:在测试中发现差距

代码覆盖率和测试覆盖率_代码覆盖率:在测试中发现差距

代码覆盖率和测试覆盖率

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.

如果您打算进行测试驱动的开发,则需要确保测试过程中正确覆盖了代码。 最初与2019.3一起发布的代码覆盖率预览包通过为您提供代码的鸟瞰图概述来帮助您确保没有错过任何内容。

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.

当与 Test Runner一起使用时 ,“代码覆盖率”包将显示 测试 的代码行,以及测试是否通过。 在 带有 Unity 2020.1 beta的 0.3.0版预览中 ,我们添加了一些改进。

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.

该软件包是 我们在Unity 2019.2中添加 的 Coverage AP I 的客户端, 并且使用此API和C#反射的组合以OpenCover格式输出测试coverage数据。 然后,程序包使用 ReportGenerato r 解析OpenCover数据,并将coverage数据显示为一系列HTML文档。

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.

要开始使用代码覆盖率,您需要 先从“程序包管理器”中 安装 预览程序包。 如果使用2020.1 beta 8或更高版本, 请记住在“ 编辑”>“项目设置”>“程序包管理器”中 启用“预览程序包

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. 

然后, 转到 编辑 > 首选项 > 常规, 然后选中 启用代码覆盖率 。 请注意,启用代码覆盖范围会增加编辑器的开销并降低性能,因此,如果不执行覆盖范围测试,建议不要将其打开。 这将使包可以访问Mono公开的代码覆盖率数据的接口。 然后,重新启动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.

测试运行完成后,该软件包将生成HTML覆盖率报告-该报告将显示总行覆盖率百分比以及您可以浏览到的类列表,以逐行表示覆盖率。

代码覆盖率和测试覆盖率_代码覆盖率:在测试中发现差距

Code Coverage report

代码覆盖率报告

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.

您如何解释结果? HTML报表中 最重要的值 将是 Line coverage 。 这 显示了测试覆盖的代码比例。 例如,如果它显示75%,则意味着您编写的所有代码行中的四分之一未被测试覆盖。 该计算考虑了测试可以覆盖哪些行。

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.

Coverage History 下的图形(屏幕截图中的红线) 显示了此项目每次测试运行的总百分比覆盖率。 力争使这个百分比尽可能高。 如果正在减少,请考虑编写更多测试以提高覆盖范围。

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.

代码覆盖率当前支持EditMode和PlayMode测试,并允许您跟踪代码覆盖率随时间的变化。 它可以与 Test Runner一起使用 。 如果您没有任何测试,它会提供 Coverage Recording 功能,可以按需捕获Coverage数据。

代码覆盖率研讨会 (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.

在我们的逐步研讨会的帮助下 开始 使用! 了解即使您没有编写任何自动化测试,也可以如何确定代码中需要更多测试的区域。 大约需要30分钟才能完成。 你可以找到它在 包管理器的样品 ,一旦你已经安装了代码覆盖率包。

You will find a Worksheet inside the Samples folder. 

您将在Samples文件夹中找到一个工作表。

代码覆盖率和测试覆盖率_代码覆盖率:在测试中发现差距

Code Coverage worksheet

代码覆盖率工作表

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​

    了解游戏代码:Shoot()函数

  • Generate a Coverage report from the PlayMode tests​

    从PlayMode测试生成覆盖率报告

  • Add Weapon tests to improve coverage​

    添加武器测试以提高覆盖率

  • Add a test for the LaserController​

    为LaserController添加测试

  • Clear the coverage data​

    清除覆盖率数据

  • Generate a Coverage report using Coverage Recording​

    使用险种记录生成险种报告

学到更多 (Learn more)

You can also watch the relevant section of our 2020.1 beta webinar to see this feature in action:

您还可以观看 我们的2020.1 Beta网络研讨会 的 相关部分, 以了解此功能的实际作用:

演示地址

For more information see the Code Coverage package documentation.

有关更多信息,请参见 Code Coverage软件包文档

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.

代码覆盖率软件包计划在2021.1中进行验证。 随时尝试一下,让我们知道您在 此论坛主题中的 想法 。

翻译自: https://blogs.unity3d.com/2020/05/22/code-coverage-spot-gaps-in-your-tests/

代码覆盖率和测试覆盖率