Test Coverage tools (Cobertura)
The test coverage tools are used to ensure that all code paths are tested. It calculates the percentage of code called by tests. It also gives reports of which parts aren’t tested.
Cobertura – Hello World
As an example we will take the Cobertura as an example. As it’s free, open source and integrated with JUnit and Ant.
Installing Cobertura
- Download cobertura from here http://cobertura.sourceforge.net/
- Add new environment variable called COBERTURA_HOME. It’s value the directory path of the library
Running the example that comes with it
- It came with an example of simple calculator that square and sum numbers
- In order to run the example you should install the JUnit & Ant & add their values in the classpath and path
- open the example’s directory from the command line and enter ant
- Now all the magic happens!! it compiles, run the tests and generate the reports
Now let’s take a look at the reports
It shows the percentage of overall the packages and for each class alone. The best thing is yet to come. By clicking on the class name it shows the class code and highlight the path which didn’t get accessed by the test cases in red and which get accessed in green. It also shows a number beside it which shows how many times this line is executed by the test cases.
How it works?
- The cobertura generate for each class an instrumented version of it which has instructions
- These instrumented version is added to the compile code
- Instead of running the normal compiled code, you run the instrumented version
- When these instructions are encountered by the Java Virtual Machine, the inserted code increments various counters so that it is possible to tell which instructions have been encountered and which have not.
No comments:
Post a Comment