确定哪些测试涵盖了一行代码 [英] Determining which tests cover a line of code

查看:71
本文介绍了确定哪些测试涵盖了一行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以确定可能执行给定代码行的单元测试集?换句话说,您不仅可以自动确定是否覆盖了给定的行,还可以自动确定覆盖该行的实际测试集?

Is there a way to determine the set of unit tests that will potentially execute a given line of code? In other words, can you automatically determine not just whether a given line is covered, but the actual set of tests that cover it?

考虑具有50K单元测试的大型代码库。显然,全部运行可能需要很长时间-数小时甚至数天。使用这样的代码库,您希望能够执行所有单元测试中的一些子集,包括仅覆盖您刚刚触摸过的一行的测试。当然,您可以手动找到并运行它们,但是我正在寻找一种更快,更全面的方法。

Consider a big code base with, say, 50K unit tests. Clearly, it could take a LONG time to run them all--hours, if not days. Working in such a code base, you'd like to be able to execute some subset of all the unit tests, including only those that cover the line (or lines) that you just touched. Sure, you could find some manually and run those, but I'm looking for a way to do it faster, and more comprehensively.

如果我正在考虑这一点正确地,这应该是可能的。工具可以静态地遍历每个单元测试中引出的所有代码路径,并提供该测试可访问的程序片段。然后(理论上)您应该能够计算在切片中包含给定行的单元测试集,这意味着该行可以由该测试执行(可以而不是将执行,因为实际的代码路径将仅在运行时根据输入或其他条件确定)。给定的代码行可能具有执行该代码的大量测试(例如,共享库中的代码),而其他代码行可能很少(或没有)覆盖它们的测试。

If I'm thinking about this correctly, it should be possible. A tool could statically traverse all the code paths leading out of each unit test, and come up with a slice of the program reachable from that test. And you should then (theoretically) be able to compute the set of unit tests that include a given line in their slice, meaning that the line could be executed by that test ("could" rather than "will" because the actual code path will only be determined at run time based on the inputs or other conditions). A given line of code could have a massive number of tests that execute it (say, code in a shared library), whereas other lines might have few (or no) tests covering them.

所以:


  1. 我对这个主意是否合理?理论上可以完成,还是我遗漏了什么?

  1. Is my reasoning sound on this idea? Could it theoretically be done, or is there something I'm leaving out?

是否已经有可以执行此操作的工具?或者,这是我没遇到过的名字的普遍现象吗?希望能找到指向Java世界中的工具或对该主题的一般研究的指针。

Is there already a tool out there that can do this? Or, is this a common thing with a name I haven't run into? Pointers to tools in the java world, or to general research on the subject, would be appreciated.


推荐答案

JetBrains的 dotCover 现在也具有.NET代码的此功能。可以通过dotCover菜单中的显示覆盖率测试选项或按Ctrl + Alt + K来访问它。

JetBrains's dotCover also now has this feature for .NET code. It can be accessed from the dotCover menu with the option "Show covering tests" or by pressing Ctrl + Alt + K.

这篇关于确定哪些测试涵盖了一行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆