什么是代码覆盖率?
我有 3 个问题:
- 什么是 CodeCoverage?
- 到底有什么好处呢 ?
- 使用什么工具 分析代码覆盖率 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有 3 个问题:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
您可以从SO网站
免费代码覆盖工具
什么是代码覆盖率以及如何衡量它?
代码覆盖率是对运行自动化测试时执行的代码行数/块/弧数的度量。CC 是通过使用专门的工具来检测要添加的二进制文件来收集的跟踪调用并对所检测的产品运行全套自动化测试。 一个好的 CC 工具不仅可以为您提供执行代码的百分比,还可以让您深入研究数据并准确查看在特定测试期间执行了哪些代码行。
代码覆盖率算法最初是为了解决通过直接查看源代码来评估源代码的问题而创建的。 代码覆盖率属于结构测试类别,因为断言是针对程序的内部部分而不是系统输出进行的。 因此,代码覆盖率的目的是找到不值得测试的代码部分。
http://www.stickyminds.com/sitewide。 asp?Function=edetail&ObjectType=ART&ObjectId=7580
替代文本 http://www.codecoveragetools.com/images/stories/software_lifecycle.jpg< /a>
它的好处
功能覆盖旨在查找执行了多少个函数或过程。
标识源代码中已执行的行数的语句或行覆盖率。
条件覆盖或决策覆盖回答了有关程序中执行的循环条件数量的问题。
路径覆盖,重点是从代码中的给定起点查找所有可能的路径。
路径覆盖,重点
入口和出口覆盖率,用于查找从开始到结束有多少个函数(C/C++、Java)或过程(Pascal)正在执行。
工具
http://www.codecoveragetools.com/
http://java-source.net/open-source/code-coverage
http://www.codecoveragetools.com/index.php/ coverage-process/code-coverage-tools-java.html
http://open-tube.com/10-code-coverage-tools-cc/
http://csharp-source.net/open-source/code-coverage
http://www.kdedevelopers.org/node/3190
You can get very good information from SO WEB SITE
Free code coverage tools
What is Code Coverage and how do YOU measure it?
Code Coverage is a measurement of how many lines/blocks/arcs of your code are executed while the automated tests are running.CC is collected by using a specialized tool to instrument the binaries to add tracing calls and run a full set of automated tests against the instrumented product. A good CC tools will give you not only the percentage of the code that is executed, but also will allow you to drill into the data and see exactly which lines of code were executed during particular test.
Code coverage algorithms were first created to address the problem of assessing a source code by looking directly at the source code. Code coverage belongs to the structural testing category because of the assertions made on the internal parts of the program and not on system outputs. Therefore code coverage aims at finding parts of the code that are not worth testing.
http://www.stickyminds.com/sitewide.asp?Function=edetail&ObjectType=ART&ObjectId=7580
alt text http://www.codecoveragetools.com/images/stories/software_lifecycle.jpg
Its Good for
Functional coverage aiming at finding how many functions or procedures were executed.
Statement or line coverage which identifies the number of lines in the source code has been executed.
Condition coverage or decision coverage answers the question about the number of loop conditions were executed in the program.
Path coverage which focuses on finding all possible paths from a given starting point in the code has been executed.
Entry and exit coverage which finds how many functions (C/C++, Java) or procedures (Pascal) were executing from the beginning to the end.
TOOLS
http://www.codecoveragetools.com/
http://java-source.net/open-source/code-coverage
http://www.codecoveragetools.com/index.php/coverage-process/code-coverage-tools-java.html
http://open-tube.com/10-code-coverage-tools-c-c/
http://csharp-source.net/open-source/code-coverage
http://www.kdedevelopers.org/node/3190
来自维基百科文章
提倡使用代码覆盖率
相关文章
代码覆盖工具的未来
软件测试中代码覆盖率工具的有效性
工具
Java 中的开源代码覆盖工具
From wikipedia article
Advocating the use of code coverage
Related articles
The Future of Code-Coverage Tools
The effectiveness of code coverage tools in software testing
Tools
Open Source Code Coverage Tools in Java
代码覆盖率是一个指标,显示源代码测试的“良好程度”。 代码覆盖率有几种类型:行覆盖率、函数覆盖率、分支覆盖率。
为了测量覆盖范围,您应手动或通过自动测试运行应用程序。
工具可以分为两类:
- 在修改的环境(如调试器)中运行编译后的代码,计算所需的点(函数、行等);
- 需要特殊编译的 - 在这种情况下,生成的二进制文件已经包含实际进行计数的代码。
有多种工具可用于测量和可视化结果,它们取决于平台、源代码的语言。
请阅读 Wikipedia 上的文章
为了向您提供工具,请定义您使用的操作系统和语言。
Code coverage is a metric, showing how "well" the source code is tested. There are several types of code coverage: line coverage, function coverage, branch coverage.
In order to measure the coverage, you shall run the application either manually or by automated test.
Tools can be divided in two categories:
- the ones that run the compiled code in a modified environment (like the debugger), counting the required points (functions, lines, etc.);
- the ones that require special compilation - in this case the resulting binary already contains the code which actually does the counting.
There are several tools for measuring and visualizing the result, they depend from platform, from source code's language.
Please read article on Wikipedia
To provide you tools, please define for which OS and language do you use.
http://en.wikipedia.org/wiki/Code_coverage
维基百科的定义非常好,但用我自己的话来说,代码覆盖率会告诉你你已经占了多少自动化测试。 100% 意味着应用程序中的每一行代码都被单元测试覆盖。
NCover 是一个 .NET 应用程序
http://en.wikipedia.org/wiki/Code_coverage
The wikipedia definition is pretty good, but in my own words code coverage tells you how much automated testing you have accounted for. 100% would mean that ever single line of code in your application is being covered by a unit test.
NCover is an application for .NET
该术语指的是您的程序被测试覆盖的程度。 有关详细信息,请参阅以下维基百科文章:
http://en.wikipedia.org/wiki/Code_coverage
The term refers to how well your program is covered by your tests. See the following wikipedia article for more info:
http://en.wikipedia.org/wiki/Code_coverage
其他答案已经涵盖了代码覆盖率是什么。 我想强调的是,您需要小心,不要将高覆盖率视为隐含地意味着您已经测试了所有场景。 它不一定说明您测试代码的情况或测试的质量,只是表示您在运行的测试中达到了一定比例的代码。
高代码覆盖率并不一定意味着高测试质量,但高测试质量确实意味着高代码覆盖率。
在实践中,我通常的目标是 90-95% 的代码覆盖率,这通常是可以实现的。 最后百分之几的价格往往太贵,不值得尝试。
The other answers already cover what Code Coverage is. The think I'd like to stress is that you need to be careful not to treat high coverage as implicitly meaning you've tested all scenarios. It doesn't necessarily say how well you've tested the code or the quality of your tests, just that you've hit a certain percentage of code as part of the tests running.
High Code Coverage does not necessarily mean High Test Quality, but High Test Quality does mean High Code Coverage
In practice, I usually aim for 90-95% code coverage which is often achievable. The last few % are often too expensive to be worth trying to hit.
开发应用程序的方法有很多种。 其中之一是“极限编程”或“测试驱动设计(TDD)”。 它指出所有代码都应该经过测试。 代码覆盖率是衡量测试量的一种方法。
我想对此发表一点评论:我不认为所有代码都应该被测试,也不应该设置特定的代码覆盖率百分比。 我也不认为代码不应该用单元测试(代码测试代码)来测试。 我确实认为人们应该决定什么是有意义的测试。 由于这个原因,我通常不使用代码覆盖率。
某些工具提供的一件事是突出显示已测试的部分。 这样,您可能会遇到一些未经测试但实际上应该经过测试的代码,这是我使用它的唯一用途。
There are many ways to develop applications. One of those is "Extreme Programming" or "Test Driven Design (TDD)". It states that all code should be tested. Code Coverage is a means of measuring how much is tested.
I'd like to make a small remark about this: I don't think all code should be tested, nor that one should set a specific percentage of code coverage. Neither do I think that code shouldn't be tested with Unit Tests (code testing code). I do think one should decide what makes sense to test. Due to this reason I generally don't use code coverage.
One thing that some tools provide, is highlight the parts that are tested. This way you might run into some code that isn't tested but actually should be, which is the only thing I use it for.
好的答案。
我的两点意见是,没有一种测试方法可以捕获所有错误,但较少的测试永远不会捕获更多的错误,因此任何测试都是好的。 在我看来,覆盖率测试并不是要显示哪些代码已经被执行过,而是显示哪些代码没有被执行过,因为那是错误喜欢潜伏的地方。
如果将其与单步执行结合起来,这是审查代码和捕获错误的非常好的方法。 这是一个示例。
Good answers.
My two cents is that there is no method of testing that catches all errors, but less testing will never catch more errors, so any testing is good. To my mind, coverage testing is not to show what code has been exercised, but to show what code has not been exercised, because that is where bugs love to lurk.
If you combine it with single-stepping, it is a very good way to review code and catch bugs. Here's an example.
我最近使用的另一个用于确保代码质量(包括代码覆盖率)的有用工具是 Sonar。
这是链接 http://www.sonarqube.org/
Another useful tool for ensuring code quality(which encompasses code coverage) that I recently used is Sonar.
Here is the link http://www.sonarqube.org/