Join Chiu-Ki Chan for an in-depth discussion in this video Code coverage, part of Effective Android Testing for Mobile Developers.
- [Instructor] Test driven development is a great way to make sure that your code is tested thoroughly. Another way is to look at code coverage. Code coverage is a tool to measure how much of your code is covered by tests broken down into class, method and line. You can generate a code coverage report any time during your development process to evaluate how much of your code is tested. To generate a code coverage report, click on the green triangle on the gutter and choose, coverage.
Here, we can see that we have 100% coverage for class and method, but only 86% for lines. Double click on it and it will open the class in question. Let's close the other tabs so that we can see this file better. Scroll down and on the side, you can see next to the line numbers either green or red. Green means that that particular line has test coverage and red mean it doesn't.
We have red in line 40 for instance. This means that we do not have coverage when the description is more than one line. You can also see that in line 44 and 45 is red which means that when we have an IO Exception we do not have a test case for that. Let's add a test method for the multi line description one. Go back to RecipeTest.Java, open the project tab and take a look at mixed.txt.
This is a recipe which has the ID and title interweaved with the rest of the lines which is our description. It also has multiple lines in the description which allow us to verify that we'll be inserting line breaks correctly. To use this SL test, go back to RecipeTest.Java and copy the whole fashion from line 10 to line 18 for the water test case. Paste it, change the test method name to, mixed.
We will be passing the recipe out of the file called, mixed.txt. So in line 22, replace water with mixed. The rest of it we are not going to do yet so comment on line 26 and 27. Run the test. It is failing because if you look at mixed.txt the ID of this recipe is, punch. Let's change that.
In line 25 change water to punch. Run the test. It passes. Next we are going to assert that the title is correct. In our case the title should be Punch with capitol letter. The test still pass. Finally, we will also assert to the description. Go to mixed.txt and copy out the description. Change assert value to what we copied out.
We'll need to delete the lines that has the word, id= in it and also title=. Run the test. Yay, it passes. Now, we are doing all this because the test coverage report told us that we have some missing lines that is not tested. So in addition to making sure that our test is passing let's also run the test coverage report. Earlier I told you to use the gutter but you can also find the same icon in the toolbar on top.
It has some bars that looks like a bar chart. Run the test with coverage by clicking on this button. You can see that our line coverage increased to 91% and when you click on this, and close the tab so that we can actually see it, line 40 has changed from red to green meaning that our test is now verifying that we are inserting new lines properly. Code coverage is a great tool to measure how much of your code is covered by tests.
However, 100% code coverage does not mean that your code is 100% correct. For instance, right now read from stream does not handle the case when the input stream is no. But code coverage would not show you that you don't have a test for a line that does not exist. In other words, do not feel a false sense of security when you obtain 100% test coverage. Always reason through your code to catch error cases and write tests for them.
We have seen how JVM test with test driven development is a great way to implement the building blocks of your app and iterate quickly. Let's move on to write test for code that involves Android with uEyeless instrument test
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP