如何发现哪个测试单元检查了哪些代码行?

发布于 2024-11-16 12:53:45 字数 187 浏览 4 评论 0原文

我在 NUint 上闲逛,希望找到一种方法来实现哪行代码通过哪个测试。

假设我有一个方法,需要进行 3 个测试。有什么方法可以找出哪个测试检查哪一行代码?

使用 Nover 后,我知道您可以找出哪些线路已经过测试,哪些线路尚未测试。但是,您确实看不到哪个单位检查了该代码。

在处理大量测试时它非常有用......

I was fooling around the NUint, hoping to discover a way to realize which line of code passes in which test.

Imagine I have a method for which I have 3 tests. Is there any way to find out which test checks which line of code?

Having used NCover, I know you can find out which lines have been tested and which have not. However, you really can't see which unit checked that code.

It can be really useful when dealing with tons of tests...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

祁梦 2024-11-23 12:53:45

JetBrains dotCover 可以帮助您获取所需的信息。
它与 ReSharper 单元测试运行程序集成并收集每个测试数据。
您可以看到每个特定测试覆盖了哪些代码(在 VS 中具有相应的突出显示)。
更有趣的是,对于每一段代码,您都可以获得覆盖它的测试列表,并轻松地重新运行它们。

此处提供更多信息:
http://blogs.jetbrains.com/dotnet/ 2010/07/show-covering-test-with-dotcover/

JetBrains dotCover can help you to get the info you are looking for.
It integrates with the ReSharper unit test runner and collects per-test data.
You can see which code is covered by the each particular test (with corresponding highlighting in VS).
And what is more interesting, for every piece of code you can get list of tests which cover it and easily rerun them.

Additional info is available here:
http://blogs.jetbrains.com/dotnet/2010/07/show-covering-test-with-dotcover/

聆听风音 2024-11-23 12:53:45

您好,因为大多数人都回答说,目前使用大多数覆盖率工具执行此操作的唯一方法是单独运行每个测试,然后查看每个测试的覆盖率。

然而这是可能的,但需要一些努力,使用 OpenCover。如果您捕获并存储来自测试和目标的所有序列点(按照它们到达的顺序),然​​后逐个测试地分析这些结果,这就是 OpenCover 的最初目标之一 - https://github.com/sawilde/opencover/wiki。目前,OpenCover 只是聚合这些结果,然后丢弃访问数据,但它们可以被存储。当然,您必须了解任何并行运行的测试(如果存在)。数据量可能令人望而生畏,这也是其尚未实施的原因之一。

Hi as most people have responded the only way to do this at the moment with most coverage tools is to run each test by itself and then view the coverage for each test.

However it is possible, but would require a bit of effort, using OpenCover. If you captured and stored all the sequence points from the tests and the targets in the order they arrive and then analyse these results on a test by test basis, this is one of the original aims of OpenCover - https://github.com/sawilde/opencover/wiki. Currently OpenCover just aggregates these results and then throws the visitation data away but they could be stored. Of course you would have to be aware of any parallel running tests should they exist. The amount of data can be daunting and is on of the reasons it has not yet been implemented.

追风人 2024-11-23 12:53:45

您可以调试您的测试。如果您使用 NUnit,您可以将该过程附加到您的 VS。

You can debug your testing. If you are using NUnit you can attach the process to your VS.

吃素的狼 2024-11-23 12:53:45

Ncover 是一个可用于计算单元测试覆盖率的工具。请参阅 NCover.com。

NCover is a tool you can use to calculate coverage of your unit tests. See NCover.com.

离鸿 2024-11-23 12:53:45

最简单的方法是下载并安装运行单元测试的 TestDriven.NET 的个人版本,并且可以选择使用 NCover 运行和报告代码覆盖率。请注意,较新的 NCover 版本是商业版本,但 TD.NET 中包含的版本有点旧,但可以免费使用。

The easiest way is to download and install a personal edition of TestDriven.NET which runs your unit tests, and has an option to run and report the code coverage using NCover. Mind you newer NCover versions are commercial but the one included in TD.NET is a little bit older but free to use.

梦屿孤独相伴 2024-11-23 12:53:45

我不知道有什么可以直接实现与您正在寻找的东西相匹配。我最近做了一个关于我们需要的自动故障定位的项目这个确切的功能,我们提出的唯一解决方案是推出我们自己的测试运行程序,该测试运行程序还收集每种方法的覆盖率信息。

使用 Visual Studio 2010(高级版和旗舰版)可能有一种间接的方法来实现此目的,它引入了 测试影响分析器。这使您可以确定哪些测试受到源代码更改的影响。但是,这仅适用于 MSTest。您可以使用这篇博文中的技术来允许您的 NUnit在 MSTest 下运行的测试。

I don't know of anything that directly achieves matches what you are looking for. I recently did a project on automated fault localization where we needed this exact functionality, and the only solution we came up with was to roll our own test runner that also gathered coverage information for each method.

There may be an indirect way to achieve this using Visual Studio 2010 (Premium and Ultimate), which introduced a Test Impact Analyzer. This allows you to determine which tests are affected changes to your source code. However, this only works for MSTest. You can use the technique in this blog post to allow your NUnit tests to run under MSTest.

寄风 2024-11-23 12:53:45

您正在寻找的术语是“代码覆盖率”。

我没有任何使用 NUnit 执行此操作的经验,但似乎有一个 codeplex 项目可用于 NUnit: http ://codecoveragerunner.codeplex.com/

如果您想查看哪个测试正在测试哪一行代码,您可以在代码行上放置一个断点并调试您的测试?当您命中断点时,

当其他测试抛出异常时,检查哪个测试是哪个测试,您可以禁用异常中断选项:

  • 在 Visual Studio 中的菜单中,转到“调试”->“调试”->“调试”。 “异常”并取消选中异常中断。那么你就不必在每次异常时都停下来。

The term you are looking for is "Code coverage".

I don't have any experience how to do this with NUnit but there seems to be a codeplex project making this available for NUnit: http://codecoveragerunner.codeplex.com/

if you want to see which test is testing which line of code you could put a breakpoint on the line of code and debug your tests? When you hit your breakpoint check which test is

when having other tests throwing exceptions you can disable the break on exception option:

  • in visual studio in the menu go to "Debug" -> "Exceptions" and uncheck the break on exceptions. then you dont have to stop at every exception.
假面具 2024-11-23 12:53:45

简单的答案是,“单独运行每个测试并收集该测试的测试覆盖率数据”。

您的组织方式可能取决于特定的测试覆盖工具以及您选择运行测试的方式。

对于我们的测试覆盖率工具,有一个明确的“TestCoverageDump”方法通过测试添加到您的软件中覆盖检测步骤。通常,对此方法的调用会插入应用程序的“主”程序中,以便当它退出时,您可以获得已运行的任何测试的测试覆盖率数据。

对于您的任务,您希望修改单元测试运行代码以在每次测试后进行显式调用
“TestCoverageDump”后跟“TestCoverageReset”(也插入),以便每个测试都有自己的向量。您选择如何将测试名称与向量关联起来完全取决于
在您的控制之下,代价是对源代码中提供的“TestCoverageDump”代码进行一点点调整。

我们的测试覆盖率显示工具可以轻松组合所有单独的向量,为您提供整体视图。或者您可以查看任何特定向量的覆盖范围。显示工具还将
让您比较不同测试的覆盖率(它们在哪里相交?一个测试什么,另一个不测试什么?)

有了每个测试的覆盖率数据,您还可以确定需要再次运行哪些测试。如果您修改代码并重新运行检测器,它会告诉您哪些测试覆盖向量(例如,哪些测试)需要根据向量先前覆盖的修改代码再次运行。

The simple answer is, "run each test by itself and collect test coverage data for that test".

How you organize that may depend on the specific test coverage tool, and how you choose to run your tests.

For our Test Coverage tools, there is an explicit "TestCoverageDump" method added to your software by the test coverage instrumentation step. Normally a call to this method is inserted in the "main" program of your application so that when it exits, you get test coverage data for whatever tests you have run.

For your task, you want to modify your unit test running code to make an explicit call, after each test, on
"TestCoverageDump" followed by "TestCoverageReset" (also inserted), so that each test gets its own vector. How you choose to associate the name of the test with the vector is completely
under you control, at the price of a tiny bit of adjustment to the "TestCoverageDump" code supplied in source.

Our test coverage display tool can easily combine all the individual vectors to give you an overall view. Or you can view the coverage for any particular vector. The display tool will also
let you compare coverage from different tests (where do they intersect? what does one test, that the other does not?)

Having this per-test coverage data also lets you determine which tests you need to run again. If you modify the code, and re-run the instrumenter, it will tell you which test coverage vectors (e.g., which tests) need to be run again based on what modified code that the vector previously covered.

鱼忆七猫命九 2024-11-23 12:53:45

如果您使用的是 Visual Studio,则应该查看 Chris Dexter 的扩展“运行 Coverlet Report”。
https://marketplace.visualstudio.com/items?itemName=ChrisDexter.RunCoverletReport

在此处输入图像描述

除了上面的综合报告之外,它还可以进行代码高亮显示,您可以看到哪些代码行哪些是完全覆盖,哪些是部分覆盖,哪些没有。

输入图片此处描述

If you are using Visual Studio, you should check out the extension "Run Coverlet Report" by Chris Dexter.
https://marketplace.visualstudio.com/items?itemName=ChrisDexter.RunCoverletReport

enter image description here

Apart from the above comprehensive report, it can also do code highlighting where you can see which code lines are fully covered, partially covered and which are not.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文