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?
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.