Improving quality with code coverage

Improving quality with code coverage

Getting many bugs even after automated testing of your product? Your tests passed but things do not work in production as expected. What could be the reason?

One of many reasons could be not all test cases are covered. There are scenarios which are being missed. It's easy to find which scenario's are missed by integrating JaCoCo (code coverage tool) with JUnit tests. JaCoCo provides analysis of each line, method and class.

  • No coverage: No instruction in the line has been executed (red background)
  • Partial coverage: Only a part of the instruction in the line have been executed (yellow background)
  • Full coverage: All instructions in the line have been executed (green background)


Here's a step by step example on how to generate code coverage reports in Java gradle project.

Pre-requisite: gradle and java is installed on your machine.

  1. Create a gradle project (I used Eclipse IDE)



2. Inside the project, you will see two folders main and test under src folder. Under main you write your development code and under test you write your unit (Junit) tests and integration tests.

3. Add below lines of code to your build.gradle file present in the root directory.

apply plugin: "jacoco" 

jacocoTestReport{
    reports {
            xml.enabled false
            csv.enabled false
            html.destination "${buildDir}/jacocoHtml"
    }
}

If JUnit dependency is not present then add it:

dependencies {
     testCompile 'junit:junit:4.12'
}

4. Create an Example.java class under the main folder.

public class Example {
	
	 public long someRandomMethod(int a, int b) {
	    	
	    	if (a > 10 && b > 10){
	    		return a+b;
	    	}
	    	else{
	    		return a-b;
	    	}
	       
	    }
}

5. To verify this method, we will write JUnit test under test folder with name ExampleTest.java 

@Test annotation tells JUnit that it is a test method to execute.

public class ExampleTest {

    @Test
	public void verifySomeRandomMethod() {
		Example junitTest = new Example();
		junitTest.someRandomMethod(11, 12);
	}
}

6. Go to the root directory of your project. And execute the below command to execute JUnit test.

gradle clean test

7. Junit results report will be generated /JunitJacocoExample/build/reports/tests/test/classes/ExampleTest.html

8. To generate code coverage report, execute below command.

gradle jacocoTestReport

9. Code coverage report will be generated /JunitJacocoExample/build/jacocoHtml/index.html

Clone this working example from https://github.com/shankybnl/junit-jacoco-example

Saarthak Vats

Tech | Fintech | SaaS | Super App | Tech leader/builder for startups

7 年

Like your pic says, quality is a habit. Tools and frameworks are mechanism to enforce and standardize a practice. But without a culture of valuing testing, it's going to be of little use.

要查看或添加评论,请登录

Shanky Sharma的更多文章

  • Death of flaky tests

    Death of flaky tests

    One of the nightmares for automation tester is intermittent false positives tests. There could be multiple reasons for…

    1 条评论

社区洞察

其他会员也浏览了