originally posted on Linkedin
It can be quite difficult to write safe software sometimes. You have to take every possible execution path into account, and you should consider every possible input. And even if you have written code that covers virtually every possible situation, you still have to test every scenario. By using code coverage tools, you can see what lines of code have been executed, and how many times these lines have been executed. By combining your unit tests with code coverage, you can make sure that every function is executed at least once.
Given the following scenario:
You are writing a program that automatically will open your windows when the temperature in your house gets too high. you Define a function for opening the window, and one for closing the window. There is also a lock on the window, to keep burglars out. The program looks as follows:
After compiling and running the code, everything looks fine, but there is one problem that you could have spotted with gcov:
The program never locks your window! In this case, a necessary function is not executed. Code coverage can help you find functions that do not execute but should have, or functions that you have not tested yet(because they are never executed). You can use code coverage reports to further strengthen your unit tests, and confirm that your application works as it should (in every possible scenario).
I wrote this article about two years ago, and have obtained much more experience in testing since. Do you have any questions about a certain subject, or would you like me to write an article about a certain subject(e.g. functional testing with selenium)? If so, then please leave a comment below. Feedback is also very welcome ;)
Top comments (0)