Using Eclemma for overlay testing

Source: Internet
Author: User

Coverage testing is an important indicator for measuring the quality of tests. After a variety of tests such as unit testing, assembly testing, integration testing, and testing of a software product, can we have some confidence in the quality of the software? This requires us to examine the quality of the tests. If the test covers only a small part of the code, no matter how many test cases we write, we cannot believe that the quality of the software is guaranteed. Conversely, if the test covers the vast majority of the software's code, we can have a reasonable confidence in the quality of the software. This article will introduce an excellent open source software testing tool, Eclemma, which can cover test programs written by the Java language to generate exhaustive coverage test reports on the results of the program's operation.

Introduced

Now more than ever, IT developers are paying more attention to the importance of testing, which is more prone to problems without well-tested code. In extreme programming, test-driven development has proven to be an effective way to improve software quality. In the test-driven development approach, software engineers write the test code first before writing the functional code, which ensures that the program code is correct from the very beginning, and that it is able to perform automated regression testing every time the program evolves. Testing is critical to the success or failure of software products, and in the field of extreme programming, it is even suggested that any untested code should be automatically removed from the released product. The author is not convinced that the idea is correct, but the quality of the test itself is really a matter of great concern. The coverage of the test is an important indicator of the quality of the test, and we need tools to help us examine the software testing coverage.

Eclemma is a great Eclipse open source plug-in that helps developers explore test coverage. Eclemma is so excellent in the area of coverage testing that it became the Eclipse Community Awards Winners finalist in the last 2006 years. While finally Eclipse Checkstyle Plugin has the title of the best Open Source eclipse-based Developer tool, we can also see that Eclemma's help for developers is huge (eclip Specific information about SE Community Award can be found in reference resources).

Mention of Eclemma first comes to the famous Java overlay testing tool Emma. Emma is an open source project on SourceForge (see Reference resources). In a way, Eclemma can be seen as a graphical interface for Emma. In the references in this article, you can see a technical article devoted to using Emma.

Emma's author has developed a variety of excellent open source Java development tools at the beginning of Emma's development. For example, we have an excellent integrated development environment Eclipse, an open source JDK, a unit test tool JUnit, a project management tool like Ant, and we can also use CVS or SubVersion for source code version maintenance. At the time, perhaps the only thing missing was an open-source coverage testing tool. Emma was born to fill the void. The situation is not the same as when Emma was born. Today, we have a lot of coverage testing tools. For example, Coverlipse is an Eclipse-based overlay test plug-in. Others are cobertura,quilt and jcoverage and so on. But Emma has some very good features that make it more suitable for widespread use. Emma is open source compared to tools such as Coverlipse, and it has a very small impact on application execution speed.

The advent of eclemma makes up for Emma's users a big pity----lack of graphical interfaces and support for the integrated development environment. Combine the two best tools of Eclipse and Emma in their respective fields, and that's what Eclemma provides for us. Next, we'll go through the next chapters with our readers to see what Eclemma has to offer developers.

Installing the Eclemma Plugin

The process of installing the Eclemma plugin is the same as most of the Eclipse plug-ins, and we can either remotely install the Eclemma plugin via the Eclipse standard Update mechanism (Figure 1), or download the zip file from the site (see Reference resources) and extract it to eclipse In the directory where you are located.

Figure 1 Adding the Eclemma update site

No matter how you install Eclemma, after the installation is complete and you restart Eclipse, a new button should appear on the toolbar:

Figure 2 The new overlay test button

Back to top of page

Testing Java programs with Eclemma

To experiment with Eclemma, we first set up a new Java project called Test.emma in Eclipse's Workspace. Next, we create a class in which the HelloWorld code looks like this:

Listing 1 The code used to test the Eclemma
Package Test.emma;public class HelloWorld {/** * @param args */public static void main (string[] args) {int rand = (int) (M Ath.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 the function through Eclemma HelloWorld.main() .

Figure 3 Overlay testing of Java applications

After execution, we are editing the Helloworld.java window will become as follows:

Figure 4 The results of the overlay test

In the Java editor, the Eclemma uses different colors to indicate the source code test situation. Where a green line indicates that the line code is fully executed, the red part indicates that the line code is not executed at all, and the yellow line indicates that the line code is partially executed. Yellow lines usually appear in the case where a single line of code contains branches, such as the 16 lines in Figure 4 are shown in yellow. Because there is a randomly determined branch in the program, the Reader's window may be slightly different from here (11 rows or 14 rows with only one red line).

In addition to coloring directly in the source editing window, Eclemma provides a separate view to count the program's coverage test rate.

Figure 5 Viewing the coverage test rate of the program

The coverage view provided by Eclemma is capable of layering the coverage test rate of the code, and the information in Figure 5 shows that we have covered approximately 68.6% of the code for a single run of HelloWorld.

It is often difficult to overwrite all of the code in a single run, and if you can combine the coverage data of multiple tests to see it, then we can more easily grasp the test results of multiple tests. Eclemma provides such a feature. Now, let's repeat the overlay test for HelloWorld several times. We notice that the coverage view always shows the most recently completed overlay test. In fact, Eclemma saved all the test results for us. Next, we'll use the Coverage view's tool buttons to combine the results of multiple overlay tests.

Figure 6 toolbar buttons for combining multiple overlay test results

After we run the Coverage multiple times, we can click the toolbar button shown in Figure 6. A dialog box is then popped up for the user to select the overlay test that needs to be merged.

Figure 7 Selecting the overlay test results that need to be merged

After the merge is complete, we can observe that the Java editor and Coverage View show the results of the merge:

Figure 8 Viewing the combined overlay test results

In Figure 8, we can see that by running the overlay test multiple times, our code eventually reaches 91.4% of the test coverage. Interestingly, the third line of code in the diagram is marked red, and this line of code is actually not executable. The trick is that we don't generate any instances of the HelloWorld class, so the default constructor is not called, and Eclemma marks the overriding state of this particular code in the first row 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 relative to the command-line version of Emma. Instead, Eclemma offers many features that are tightly integrated with Eclipse. Not only does it test Java application, it also calculates JUnit unit tests that cover the Eclipse plug-in test. We can see from Figure 9 that Eclemma currently supports four types of programs.

Figure 9 Configuration page for Eclemma

To understand how Eclemma is getting coverage test data, we need to have a preliminary understanding of Emma first. It is common for code to overwrite the test tool to modify the code being executed. And Emma offers two ways to do it.

    1. Pre-insertion mode: Before you can measure a program, you need to modify the class file or jar file with the tools provided by Emma. The code after the modification is completed can be executed immediately. The results of the overwrite test will be stored in the specified file.
    2. Instant Insert mode: Instant insert mode requires no prior modification of the code. Instead, the code was modified by a Emma-customized class loader (the loader). The advantages of this approach are obvious, and we do not need to make any modifications to the class or jar files. The downside is that we need to execute the Java application with the command Emmarun provided by Emma in order to get the results of the test.

The advantage of using instant Insert mode is obvious: class files and jar files are not modified. The pre-insertion mode is more widely used, and we can only use pre-insertion mode for some code that needs to be embedded in the framework (for example, EJBS). Eclemma only uses Emma's pre-insertion mode to work, but the Eclemma lacks the capital to create a copy of the class and jar files in the temp directory to modify, so the class and jar files remain intact in workspace. It sounds good, but because you need to modify Classpath to use modified class and Jar files, for applications that cannot modify classpath (for example, Eclipse RCP and JUnit Plugin Test), we can only choose to modify Wo The class file and the jar file in the rkspace. For the Java application and JUnit type overlay tests, we can select the "in-place instrumentation" Item in the Configuration dialog box to specify that the. class file and. jar in Workspace be modified directly. File.

Conclusion

This article introduces a simple example of the basic process of using ECLEMMA for coverage testing. Eclemma allows software engineers to easily examine the coverage of tests and to show the results to developers in an intuitive and concise manner.

Using Eclemma for overlay 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.