I use Istanbul for code coverage of unit tests in an AngularJS project. There are 4 types of coverage and they are
Statement, function and line are alright but i don't understand what "branch" is. What is branch?
Branch coverage is a testing method, which aims to ensure that each one of the possible branch from each decision point is executed at least once and thereby ensuring that all reachable code is executed. That is, every branch taken each way, true and false.
A branch exists for any block, line, statement, or part of a statement which may be skipped during execution. This can be the result of if-then-else, switch-case-default, or the ternary operator.
Branch Coverage (%) = 3/3 * 100, which results in 100% coverage. Explanation: The main purpose of the Branch Coverage Testing is to make sure every functional test scenario has the ability to cover all the potential branching for functional looping statements.
The "Branch" column shows the branches that have been executed (✔) or skipped (x). This corresponds to the entries in the GCOV file. The "Exec" column shows how often a certain line has been executed. The "Source" column shows the actual source code.
A branch is where the runtime can choose whether it can take one path or another. Lets take the following example:
if(a) { Foo(); } if(b) { Bar(); } Yay();
When reaching the first line, it can decide if it wants to go inside the body of the if(a)
-statement. Also, it can decide not to do that. At this stage, we've seen two paths already (one branch).
The next statement after that gets more interesting. It can go inside the if
body and execute Bar
. It can also not do that. But remember that we've already had a branch before. The result may vary if Foo
was called or not.
So we end up with four possible paths:
Foo
, not calling Bar
eitherFoo
, not calling Bar
Foo
, calling Bar
Foo
and Bar
The last Yay
is always executed, no matter if Foo
or Bar
was called, so that doesn't count as a branch. So the code snippet above contains four paths / two branches (calling Foo()
or not, calling Bar()
or not).
Like other answers already have mentioned, there are numerous statements that can cause a branch (if
/switch
). A few, but not all, include:
while
/for
/do-while
)break
or the continue
statement&&
/||
) foo && bar
, if foo
is false
, bar
doesn't have to be evaluated. Likewise for foo || bar
, if foo
is true
, bar
doesn't have to be evaluated either.function A(someVar = []) { ... }
) if
-statement. For every call to this method the parameter is checked if its undefined
. If it is, the default parameter is set for someVar
(for the example above). This causes a branch to exist.The code coverage tool wants to make sure that you've tested all branches. Best would be if all paths have been tested, or even all (edge case) values, not just the branches. This, to make sure that no unwanted behavior is executed.
Somewhere where the code can take more than one route, ie it branches. A couple of examples of branching statements are if
/else
and switch
statements.
Branch coverage tracks which of those branches have been executed so you can ensure all routes are tested properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With