Code coverage

We did not use code coverage tools throughout this exercise. The reason is that we wanted you to be focused on the Red-Green-Refactor model. You wrote a test, saw it fail, wrote the implementation code, saw that all the tests were executed successfully, refactored the code whenever you saw an opportunity to make it better, and then you repeated the process. Did our tests cover all cases? That's something that code coverage tools such as JaCoCo can answer. Should you use those tools? Probably, only in the beginning. Let me clarify that. When you are starting with TDD, you will probably miss some tests or implement more than what the tests defined. In those cases, using code coverage is a good way to learn from your own mistakes. Later on, the more experienced you become with TDD, the less of a need you'll have for such tools. You'll write tests and just enough of the code to make them pass. Your coverage will be high with or without tools such as JaCoCo. There will be a small amount of code not covered by tests because you'll make a conscious decision about what is not worth testing.

Tools such as JaCoCo were designed mostly as a way to verify that the tests written after the implementation code are providing enough coverage. With TDD, we are taking a different approach with the inverted order (tests before the implementation).

Still, we suggest you use JaCoCo as a learning tool and decide for yourself whether to use it in the future.

To enable JaCoCo within Gradle, add the following to build.gradle:

apply plugin: 'jacoco'

From now on, Gradle will collect JaCoCo metrics every time we run tests. Those metrics can be transformed into a nice report using the jacocoTestReport Gradle target. Let's run our tests again and see what the code coverage is:

$ gradle clean test jacocoTestReport

The end result is the report located in the build/reports/jacoco/test/html directory. Results will vary depending on the solution you made for this exercise. My results say that there is a 100% of instructions coverage and 96% of branches coverage; 4% is missing because there was no test case where the player played on a box 0 or negative. The implementation of that case is there, but there is no specific test that covers it. Overall, this is a pretty good coverage:

JaCoCo will be added in the source code. This is found in the 05-jacoco branch of the tdd-java-ch03-tic-tac-toe Git repository at https://bitbucket.org/vfarcic/tdd-java-ch03-tic-tac-toe/branch/05-jacoco.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.175