About Code Coverage testing

Source: Internet
Author: User

Start to hear code coverage this word only feel not strange but as to what is the meaning of where to see is not very remember, when I began to study the code coverage testing tools to further check, it is understood that code coverage refers to: a measure of the extent of code coverage, is the scale and extent of the source code being tested in the description program. Perhaps this is still somewhat abstract. Below is a concrete demonstration of code coverage through common metrics and test tools for code coverage:

First, commonly used indicators

1 , statement overrides: Whether each executable statement in the code being tested is executed, the statement overrides are "weakest overrides", and the code for some control structures does not really represent whether they are fully covered.

2 , decision coverage: whether each of the determined branches in the measurement program is tested.

3 , conditional overrides: The structure of each sub-expression in a metric decision true and the false whether it was tested to.

Note: Because the decision coverage and condition coverage are not image-aware, a simple comparison of the decision coverage and conditional coverage:

int foo (int a, int b)

{

if (A < | | b < 10)//judgment

{

return 0; Branch One

}

Else

{

return 1; Branch Two

}

}

The decision overlay only needs to consider the result as true and the false Two types of cases:

testcaes1:a = 5, b = any number covers the branch one

testcaes2:a = A, B = covers the branch two

Conditional overrides only need to consider the results of each conditional expression in the decision:

TestCase 3 : a = 5, B = 5 true, True

Testcase4:a = b=, False

the difference between a decision overlay and a conditional overlay can be known through an instance: The decision overlay is a requirement to overwrite each branch statement, and the conditional override simply evaluates the conditional expression true and the false It's all ready to be tested.

4 , Path overrides: Measures whether each branch of the method is executed, that is, requires that all possible branches be executed once. Path overrides are also referred to as the strongest overrides.

In fact, there are many kinds of coverage, here is no longer detailed introduction.

Second, Code Coverage test report

since the project is usedmavenManagement, Self -mavenjust knows, the tools for unit testing just know a little bit of fur, so they are slowly groping, remember to look atmavenwhen the video heard about the use of testing tools, so went back to see, and finally chose to use the firstCoberturaTest Tool Try it:Coberturais an open source tool that measures test coverage by detecting basic code and observing what code was executed while the test package was running and what code was not executed, in addition to locating the bit-tested code and discoveringBugOutside,CoberturaIt can also be done by marking the useless. Code that cannot be executed to optimize the code.

in the maven in fact, it is very convenient to use in Pom the file is adding the appropriate reference:

<!--code Coverage test version number control ****** Chenlina **start****--><cobertura.version>2.6</cobertura.version> <!-- Code coverage test version number control ****** Chenlina **end*****-<!--perform unit test plug-in ********* Chenlina ******start***2015-2-10*****--><plugi n><groupid>org.apache.maven.plugins</groupid><artifactid>maven-surefire-plugin</ artifactid><version>${maven-surefire-plugin.version}</version><configuration>< forkcount>3</forkcount><reuseforks>true</reuseforks><testfailureignore>true</ testfailureignore></configuration></plugin> <!--perform unit test plug-in ********* Chenlina ******end***2015-2-10** -<!--code generation coverage report ******* Chenlina ****start*******2015-2-10************-&LT;PL Ugin> <groupId>org.codehaus.mojo</groupId> <artifactid>cobertura-maven-plugin</artif Actid> <version>${cobertura.version}</version> <configuration> <formats> <format>html</format> <format>xml</format > </formats> </configuration> <executions> &                    Lt;execution> <id>cobertura</id> <phase>test</phase>                    <goals> <goal>cobertura</goal> </goals> </execution> </executions> </plugin> <  !--Code Generation Coverage report ******* Chenlina ****end********2015-2-10************-

so after the execution Test you can then generate the test report at the same time Target of the site generate code Coverage test reports at the same time :



Open Index you can see the Code share probability report:


Click in to see the specific code coverage:


see this in a blog post: Cobertura is an important new tool in the Agile Programmer's Toolkit. By generating specific values for code coverage,Cobertura transforms unit testing from an art into a science. It can look for gaps in the test coverage and find the bug directly. Measuring code coverage allows you to get the information you need to find and fix bugs to develop software that is more robust for everyone.

About Code Coverage testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.