什么是代码覆盖率?

发布于 2024-07-27 18:24:14 字数 108 浏览 6 评论 0 原文

我有 3 个问题:

  • 什么是 CodeCoverage?
  • 到底有什么好处呢 ?
  • 使用什么工具 分析代码覆盖率 ?

I have 3 questions :

  • What is CodeCoverage ?
  • What is it good for ?
  • What tools are used for
    analyzing Code Coverage ?

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

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

发布评论

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

评论(9

与往事干杯 2024-08-03 18:24:14

您可以从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>

它的好处

  1. 功能覆盖旨在查找执行了多少个函数或过程。

  2. 标识源代码中已执行的行数的语句或行覆盖率。

  3. 条件覆盖或决策覆盖回答了有关程序中执行的循环条件数量的问题。

  4. 路径覆盖,重点是从代码中的给定起点查找所有可能的路径。

    路径覆盖,重点

  5. 入口和出口覆盖率,用于查找从开始到结束有多少个函数(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

  1. Functional coverage aiming at finding how many functions or procedures were executed.

  2. Statement or line coverage which identifies the number of lines in the source code has been executed.

  3. Condition coverage or decision coverage answers the question about the number of loop conditions were executed in the program.

  4. Path coverage which focuses on finding all possible paths from a given starting point in the code has been executed.

  5. 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

羁拥 2024-08-03 18:24:14

来自维基百科文章

代码覆盖率是一种衡量标准
软件测试。 它描述了
源代码的程度
程序已经过测试。 它是一种形式
检查代码的测试
直接,因此是一种形式
白盒测试1。 目前,
代码覆盖率的使用扩展到
数字硬件领域,
当代设计方法论
这依赖于硬件描述
语言(HDL)。

提倡使用代码覆盖率

代码覆盖率工具只需保留
跟踪代码的哪些部分得到了
已执行,哪些部分未执行。

通常,结果是细化的
到每行代码的级别。 所以
在典型情况下,您启动
您的应用程序具有代码覆盖率
配置来监视它的工具。 什么时候
您退出应用程序、工具
将生成代码覆盖率报告
它显示了哪些代码行
哪些被执行,哪些没有被执行。 如果
你计算总行数
被执行并划分为
可能有的总行数
被处决后,你会得到一个百分比。
如果您相信代码覆盖率,
百分比越高越好。 在
练习,达到100%是非常了不起的
罕见。

代码覆盖率工具的使用是
通常结合使用一些
一种自动化测试套件。 没有
自动化测试、代码覆盖率
工具只是告诉您哪些功能
人类用户记得使用。 这样一个
工具在使用时会更加有用
衡量测试套件的完成程度
是关于你拥有的代码
写的。

相关文章

代码覆盖工具的未来

软件测试中代码覆盖率工具的有效性

工具

Java 中的开源代码覆盖工具

From wikipedia article

Code coverage is a measure used in
software testing. It describes the
degree to which the source code of a
program has been tested. It is a form
of testing that inspects the code
directly and is therefore a form of
white box testing1. Currently, the
use of code coverage is extended to
the field of digital hardware, the
contemporary design methodology of
which relies on Hardware description
languages (HDLs).

Advocating the use of code coverage

A code coverage tool simply keeps
track of which parts of your code get
executed and which parts do not.

Usually, the results are granular down
to the level of each line of code. So
in a typical situation, you launch
your application with a code coverage
tool configured to monitor it. When
you exit the application, the tool
will produce a code coverage report
which shows which lines of code were
executed and which ones were not. If
you count the total number of lines
which were executed and divide by the
total number of lines which could have
been executed, you get a percentage.
If you believe in code coverage, the
higher the percentage, the better. In
practice, reaching 100% is extremely
rare.

The use of a code coverage tool is
usually combined with the use of some
kind of automated test suite. Without
automated testing, a code coverage
tool merely tells you which features a
human user remembered to use. Such a
tool is far more useful when it is
measuring how complete your test suite
is with respect to the code you have
written.

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

乄_柒ぐ汐 2024-08-03 18:24:14

代码覆盖率是一个指标,显示源代码测试的“良好程度”。 代码覆盖率有几种类型:行覆盖率、函数覆盖率、分支覆盖率。

为了测量覆盖范围,您应手动或通过自动测试运行应用程序。

工具可以分为两类:
- 在修改的环境(如调试器)中运行编译后的代码,计算所需的点(函数、行等);
- 需要特殊编译的 - 在这种情况下,生成的二进制文件已经包含实际进行计数的代码。

有多种工具可用于测量和可视化结果,它们取决于平台、源代码的语言。

请阅读 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.

巷子口的你 2024-08-03 18:24:14

代码覆盖率是软件测试中使用的一种衡量标准。 它描述了程序源代码经过测试的程度。

http://en.wikipedia.org/wiki/Code_coverage

维基百科的定义非常好,但用我自己的话来说,代码覆盖率会告诉你你已经占了多少自动化测试。 100% 意味着应用程序中的每一行代码都被单元测试覆盖。

NCover 是一个 .NET 应用程序

Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested.

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

哥,最终变帅啦 2024-08-03 18:24:14

该术语指的是您的程序被测试覆盖的程度。 有关详细信息,请参阅以下维基百科文章:

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

爱*していゐ 2024-08-03 18:24:14

其他答案已经涵盖了代码覆盖率是什么。 我想强调的是,您需要小心,不要将高覆盖率视为隐含地意味着您已经测试了所有场景。 它不一定说明您测试代码的情况或测试的质量,只是表示您在运行的测试中达到了一定比例的代码。

高代码覆盖率并不一定意味着高测试质量,但高测试质量确实意味着高代码覆盖率。

在实践中,我通常的目标是 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.

世态炎凉 2024-08-03 18:24:14

开发应用程序的方法有很多种。 其中之一是“极限编程”或“测试驱动设计(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.

两相知 2024-08-03 18:24:14

好的答案。

我的两点意见是,没有一种测试方法可以捕获所有错误,但较少的测试永远不会捕获更多的错误,因此任何测试都是好的。 在我看来,覆盖率测试并不是要显示哪些代码已经被执行过,而是显示哪些代码没有被执行过,因为那是错误喜欢潜伏的地方。

如果将其与单步执行结合起来,这是审查代码和捕获错误的非常好的方法。 这是一个示例。

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.

情域 2024-08-03 18:24:14

我最近使用的另一个用于确保代码质量(包括代码覆盖率)的有用工具是 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/

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