Test Coverage –Why does a tester needs to know this? For any QA, the perception of having more than 90% test coverage is believed to be the best. This ensures that the complete package has been exercised. It indicates that the complete set of test cases is delivered and executed w.r. to any given functionality or user story.
How do we achieve this? This simple example given below will help you understand the concept.
The flowchart:
- All entries, exits, decisions and each statement of code represents Nodes “1, 2, 3, 4, 5, 6, 7”.
- All branching and non-branching links between nodes represents Edges “A, B, C, D, E, F, G, H, I”.
Statement Coverage (SC):
Definition: This is a simple and fundamental metric for test coverage which states that every statement of the code is executed at least once. This implies the flow of coverage control reached every executable statement.
Solution: Identify the shortest number of paths with which all the nodes will be covered at one go.
Hence for the above example, the statement coverage is 2 because, by traversing through the path 1A- 2C- 4E- 7 and 1B-3D-4F-5G-6 all the nodes are covered.
Branch Coverage (BC):
Definition: Measured by the fraction of every independent code segment that is being executed. This ensures every procedure, sub-procedure, decision making statements etc are been executed at least once, to ensure the complete branch is being covered.
Solution: Identify the minimum number of paths required to cover all the edges. For the above example, we can’t cover up the entire functionality in one path.
Byfollowingpaths1A- 2C- 4E- 7 and 1B-3D-4F-5G-6,maximum numbers of edges(1, 2, 3, 4, 5, 6, and 7)are covered, however the edges H and I still needs to be covered.
To cover up these edges we can follow 1A-2C-4F-5G-6H-5I.Combining both of the paths above, the complete flow is covered including the decision making paths. Hence the branch coverage for this is 3.
Path Coverage (PC):
Definition: Every control paths and loop paths are executed at least once. Based on the logical complexity of procedures and design, the test cases are created. This is a guaranteed coverage where in the complete flow is covered.
Solution: This ensures to traverse through all the paths from start to end. The possible paths are:
1A-2C-4E-7
1B-3D-4E-7
1A-2C-4F-5G-6H-5I-7
1B-3D-4F-5G-6H-5I-7
So the path coverage for the above example is 4.
Thus for the above example Statement Coverage is 2, Branch Coverage is 3 and Path Coverage is 4.
Please note that 100%Branch/Decision coverage will mean 100% Statement coverage.
- Path coverage will mean 100% Branch/Decision coverage and 100 % statement coverage
This technique empowers developers to create a healthy code base and do their unit testing completely and deliver to QA. In turn QA team, can use the above techniques to ensure complete test coverage is done and delivery a quality product altogether.
Hi Victa Shiny, its a nice post about Test Coverage. I like to know more on it. Thanks.