有没有一种方法可以在不运行代码的情况下确定代码覆盖率?
我不是在问 StyleCop 或 Fxcop 提供的静态代码分析。两者都有不同的目的,但效果很好。我问是否有办法找到您的用户控件或子模块的代码覆盖率?例如,您有一个应用程序在单独的程序集中使用辅助类。为了确保单元测试代码覆盖率,我们需要运行应用程序并确保使用NCover或类似工具。
我的要求是,在不运行它的情况下,是否有可能找到帮助器类或类似类型的程序集的代码覆盖率?
I am not asking the static code analysis which is provided by StyleCop or Fxcop. Both are having different purpose and it serves well. I am asking whether is there a way to find the code coverage of your user control or sub module? For ex, you have an application which uses the helper classes in a separate assembly. Inorder to ensure the unit testing code coverage, we need to run the application and ensure using NCover or similar tool.
My requirement is, without running it, is there any possible to find code coverage of the helper classes or similar kind of assemblies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅测试覆盖率静态估计,了解以下技术:在不执行源代码的情况下估计覆盖率。
基本思想是为每个测试用例计算一个程序片段,然后“计算”什么切片枚举。 (前向)切片实际上是程序的一部分,您可以从代码中的特定起点(在本例中为测试代码)到达。
虽然如果您不是 ACM 会员 [或者您没有参加发表该技术的会议 :],则很难获得上述技术论文,但有一个 此处的幻灯片演示。
当然,运行这个静态估计器只会(粗略地)告诉您将执行哪些代码。它不能替代实际运行测试并验证它们是否通过!
See Static Estimation for Test Coverage for a technique that estimates coverage without executing the source code.
The basic idea is to compute a program slice for each test case, and then "count" what the slice enumerates. A (forward) slice is effectively that part of a program that you can reach from a specific starting point in the code, in this case, the test code.
While the technical paper above is hard to get if you're not an ACM member [or you didn't attend the conference where it was presented :], there's a slide presentation here.
Of course, running this static estimator only tells you (roughly) what code will be exercised. It doesn't substitute for actually running the tests, and verifying that they pass!
一般来说,答案是否定的。这相当于不可计算的停止问题。
In general, the answer is no. This is equivalent to the halting problem, which is not computable.
有一些基于抽象解释或模型检查的(研究)工具可以显示语言子集的覆盖属性,而无需执行。请参阅
“分析有界模型检查中的功能覆盖率”,Grosse, D. Kuhne, U. Drechsler, R. 2008
一般来说,是的,有一些方法,但它们是专门化的,并且可能需要一些正式方法经验。这种东西仍然是前沿研究。
There are (research) tools based on abstract interpretation or model checking that can show coverage properties without execution, for subsets of language. See, e.g.
"Analyzing Functional Coverage in Bounded Model Checking", Grosse, D. Kuhne, U. Drechsler, R. 2008
In general, yes, there are approaches, but they're specialized, and may require some formal methods experience. This kind of stuff is still cutting edge research.
我会说不;编译器可以确定的“死代码”除外。
我对代码覆盖率的定义是一个结果,它指示程序中每行代码运行了多少次:当然,这意味着运行程序。这里的决定因素通常是通过程序的数据值,它们决定条件语句所采取的执行路径。静态分析(如编译器)可以推断出在任何条件下都无法运行的代码行。
这里的一个例子是,如果你的程序使用了第三方库,但库中存在错误。如果您的程序从不使用库的这些部分,或者您发送到库的数据使其避免了错误,那么您将不会受到影响。
您可以编写一个程序,通过反射,假设将采用所有条件,并通过所有派生类跟踪所有函数调用,但我不确定这会告诉您什么。它当然无法告诉您所涵盖的代码行中是否存在任何错误。
I would say no; with the exception of 'dead code' which a compiler can determine.
My definition of code coverage is a result which indicates how many times each line of code is run in your program: which, of course, means running the program. The determining factor here is usually the values of data passing through the program which the determine the paths of executions taken by conditionals. A static analysis, like a compiler, could deduce lines of code that cannot run under any conditions.
An example here is if your program uses a third-party library, but there is a bug in the library. If your program never uses those parts of the library, or the data you send to the library causes it to avoid the bug, then you won't be affected.
You could write a program that, by reflection, assumes that all conditionals will be taken, and follows all function calls, through all derived classes, but I'm not sure what this will tell you. It certainly can't tell you whether or not there are any bugs in the lines of code covered.
Coverity 静态分析是一个可以识别程序中许多安全缺陷的工具。它还可以识别死代码,并可用于帮助满足 D0178B 等测试法规,该法规要求开发人员证明所有代码都可以执行。
Coverity Static Analysis is a tool that is can identify many secuirty flaws in a program. It can also identify dead code and can be used to help satisfy testing regulations such as D0178B which requires that the developers demonstrate that all code can be executed.
如果您使用的是 Visual Studio,您可以先运行“分析代码覆盖率”,然后您可以使用 Visual Studio 中的以下按钮(以绿色标记)导出代码覆盖率结果:
稍后您可以导入 Coverage结果文件返回到 Visual Studio
If you are using Visual Studio, you can first run 'Analyze Code Coverage', Then you can export code Coverage results using below Button(marked in Green) in Visual Studio:
Later you can import the Coverage Result file back to Visual Studio