Measuring test coverage with Emma

Source: Internet
Author: User
Tags command line

Describes the importance of test code coverage

Test-driven Development (TDD) is an important feature of extreme programming, and it has many advantages and is accepted by more and more developers. During the test-driven development process, the programmer went through the process of writing test cases, implementing functions, and refactoring the code in a continuous iteration. Practice has shown that this process can significantly improve our productivity and produce high quality code. It can also give us self-confidence, let us rest assured that the refactoring of their own code.

The test code does guarantee the quality of the code, but if you think you've written a bunch of test cases and can run through it, then you're wrong. The hidden bugs may just be waiting for the moment to crash your system. What's the reason? Smart you must have thought that the test code is used to ensure the quality of the functional code, but the quality of the test code, we do not know. We need to know how much of the functional code we've been working on, and that's the starting point for writing this article, and I'm going to introduce a tool for testing code coverage-EMMA.

Introducing EMMA

EMMA is an open source tool for detecting and reporting JAVA code coverage. Not only is it good for small projects, it's easy to get coverage reports, and it works for large enterprise-level projects.

EMMA has many advantages, first you can get it for free, and use it for the development of your own project. It supports a number of levels of coverage metrics: packages, classes, methods, statement blocks (basic block), and rows, especially when it can measure whether a row is partially overwritten, such as a conditional statement short circuit. It generates text,xml,html and other forms of reporting to meet different requirements, and its HTML reports provide Drill-down capabilities, and we are able to start with a step-by-step link from package to one of our concerns. EMMA can integrate with Makefile and Ant for easy application in large projects. In particular, it is important to note that EMMA is very efficient, and this is essential for large projects.

EMMA is tracking the running code information by inserting a byte code into the. class file. EMMA supports two modes: on the Fly and Offline mode.

On the fly mode adds a byte code to the loaded class, which is equivalent to the Application class loader that EMMA implements to replace the original application class loader.

Offline mode adds a byte code before the class is loaded.

On the fly mode is more convenient, and the disadvantages are obvious, such as it can not generate coverage reports for classes loaded by the Boot class loader, or generate coverage reports for classes like Java EE containers that own unique class loader. At this point, we can turn to Offline mode.

EMMA also supports two ways of running: Command line and Ant.

The command line is generally available with on the fly mode and can quickly generate coverage reports for simple projects. Running EMMA with an Ant task is especially useful for large projects.

The examples provided later in this article are primarily to demonstrate how to integrate EMMA and Ant and generate coverage reports through the Offline pattern.

Sample Project

The example engineering Sampleproject is a small project with a class of numberparser, the main function is to parse a string into a float type. Here is the directory structure for the entire project.

Figure 1. Directory structure for sample projects

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.