Android basics 12: Android automated testing 07-eclemma coverage testing

Source: Internet
Author: User

Coverage testing is an important indicator to measure the testing quality. Can we have a certain degree of confidence in the quality of a software product after a wide range of tests, such as unit testing, assembly testing, integration testing, and acceptance testing? This requires us to examine the quality of the test. If the test only covers a small part of the code, no matter how many test cases we write, we cannot believe that the software quality is guaranteed. On the contrary, if testing covers the vast majority of software code, we can have a reasonable confidence in the quality of the software. This article describes an excellent open-source software testing tool.
Eclemma, which can perform coverage tests on programs written in Java, so as to generate a detailed coverage test report on the results of program running.

Introduction

Now it developers are more concerned about the importance of testing than ever before, and code without good tests is more prone to problems. In extreme programming, test-driven development has proved to be an effective way to improve the quality of software. In the test-driven development mode, the software engineer first writes the test code before writing the functional code. This ensures the correctness of the program code from the very beginning, in addition, automatic regression testing can be performed during each evolution of the program. Testing plays a vital role in the success or failure of software products. In the extreme programming field, some people even suggested that any untested code should be automatically deleted from the released product. The author is not sure that this view is correct, but the quality of the test itself is indeed a matter of high attention. Testing coverage is an important indicator of testing quality. We need tools to help us evaluate the coverage of software testing.

Eclemma is an excellent Eclipse open-source plug-in that can help developers evaluate test coverage. Eclemma is so outstanding in the coverage testing field that it became the final player for eclipse Community Awards Winners in the last few years. Although eclipse checkstyle plugin won the title of best open source eclipse-based developer tool, we can also see that eclemma is of great help to developers (eclipse
For more information about Community Award, see reference resources ).

When talking about eclemma, we should first talk about the famous java coverage testing tool Emma. Emma is an open-source project on SourceForge (see reference resources ). To some extent, eclemma can be seen as a graphical interface of Emma. In the references in this article, we can see that
Emma technical article.

At the beginning of Emma's development, programmers already had a variety of excellent open-source Java development tools. For example, we have an excellent integrated development environment eclipse, an open source JDK, a unit test tool JUnit, and a project management tool like ant, we can also use CVs or subversion for source code version maintenance. At that time, perhaps the only thing missing was an open-source coverage testing tool. Emma is born to fill this gap. The current situation is different from that when Emma was born. Today, we already have a lot of coverage testing tools. For example, coverlipse
Is an eclipse-based coverage test plug-in. Other features include cobertura, quilt, and jcoverage. However, Emma has some excellent features that make it more suitable for widespread use. Compared with tools such as coverlipse, Emma is open-source and has little impact on application execution speed.

The emergence of eclemma makes up for a big regret for Emma users-lack of graphical interfaces and support for integrated development environments. Combining eclipse and Emma, the two best tools in their respective fields, is what eclemma provides for us. Next, let's take a look at what eclemma provides to developers in subsequent chapters.

Install eclemma plug-in

The process of installing the eclemma plug-in is the same as that of most Eclipse plug-ins. We can remotely install the eclemma plug-in through the eclipse Standard update mechanism (figure
1), you can also download from the site (see reference resources)
ZIP file and decompress it to the eclipse directory.

Figure 1 add an eclemma Update site


Name: eclemma
URL: http://update.eclemma.org/

No matter which method is used to install eclemma, after the installation is complete and eclipse is restarted, a new button should appear on the toolbar:
Figure 2 new overwrite test button


Use eclemma to test Java programs
To test the features of eclemma, we first create a new Java project named test. Emma in the eclipse workspace. Next, we create a helloworld class in it. The Code is as follows:
Listing 1 code for testing eclemma

                package test.emma;public class HelloWorld {/** * @param args */public static void main(String[] args) {int rand = (int) (Math.random()*100);if(rand%2==0){System.out.println( "Hello, world! 0");}elseSystem.out.println("Hello, world! 1");int result = rand%2==0? rand+rand:rand*rand; System.out.println(result);}} 

Next, we run it through eclemmaHelloWorld.main()Function.
Figure 3 coverage test for Java applications

After the execution is complete, the window for editing helloworld. Java will become as follows:

Figure 4 overwrite test results


In the Java Editor, eclemma uses different colors to mark the test of source code. The green line indicates that the line of code is fully executed, the red part indicates that the line of code is not executed at all, and the yellow line indicates that the line of code is partially executed. A yellow line usually appears when a single line of code contains a branch, for example, in figure 4
Lines 16 are yellow. Because the program has a random branch, the reader's window may be slightly different from here (11 rows or 14 rows have only one red row ).

In addition to directly coloring in the source code editing window, eclemma also provides a separate view to measure the coverage test rate of the program.

Figure 5 program coverage test rate


The coverage view provided by eclemma can display the code coverage test rate hierarchically. The information in Figure 5 shows that we overwrite about 68.6% of the code for one operation of helloworld.

It is usually difficult to overwrite all the code in one run. If we can combine the data covered by multiple tests for observation, then we can more easily master the test results of Multiple tests. Eclemma provides this function. Now, let's repeat the coverage test of helloworld several times. We noticed that the coverage view always shows the last completed coverage test. In fact, eclemma saves all the test results for us. Next, we will use the coverage View tool button to combine the results of multiple coverage tests.

Figure 6 toolbar buttons used to overwrite the test results multiple times

After we run coverage multiple times, we can click the Graph
6. toolbar buttons. Then, a dialog box is displayed for you to choose the coverage test to be merged.

Figure 7 overwrite test results to be merged


After merging, we can see that the merged results are displayed in both the Java editor and the coverage View:

Figure 8 view the combined coverage test results


Figure
8, we can see that through multiple run coverage tests, our code eventually reached 91.4% test coverage. Interestingly, the third line of code in the figure is marked red, and this line of code is actually not executable. The secret is that we didn't generate any helloworld class instances, so the default constructor is not called, and eclemma marks the overwriting status of this special code in the first line of the class declaration.


Advanced features of eclemma
If eclemma can only test the test coverage of Java application, it does not provide much enhancement compared with the command line version of Emma. On the contrary, eclemma provides many functions closely integrated with eclipse. It can not only test the Java application, but also calculate the coverage rate of the JUnit unit test and the Eclipse plug-in test. As shown in Figure 9, eclemma currently supports four types of programs.
Figure 9 eclemma configuration page


To understand how eclemma obtains coverage test data, we need to first have a preliminary understanding of Emma. Generally, code overwrite testing tools need to modify the executed code. Emma provides two methods to accomplish this.

  1. Pre-insert mode:Before measuring the program, you must use the tool provided by Emma to modify the class file or jar file. The modified code can be executed immediately. Overwrite test results will be stored in the specified file.
  2. Instant insertion mode:In the instant insertion mode, you do not need to modify the code in advance. On the contrary, the code is modified through a class loader customized by Emma. This method has obvious advantages. We do not need to modify the class or jar file. The disadvantage is that we need to use the emmarun Command provided by Emma to execute Java applications to obtain test results.

The advantage of using the instant insertion mode is obvious: the class and jar files will not be modified. The pre-insert mode is more widely used. For some code that needs to be embedded in the framework (such as EJB), we can only use the pre-insert mode. Eclemma only uses the Emma pre-insertion mode, but eclemma creates a copy of the class file and JAR file in the temporary directory by default for modification, therefore, the class and jar files remain unchanged in the workspace. Although it sounds good, You need to modify the classpath to use the modified class and jar files.
For classpath applications (such as Eclipse RCP and JUnit plugin test), we can only modify the class and jar files in the workspace. For Java application and JUnit coverage tests, we can select"In-Place instrumentation"To directly modify the. Class and. Jar files in the workspace.

References:

Use eclemma for 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.