Testing Code Coverage

When we’re testing our code, it’s often nice to see not only which lines of code are executed a lot but also which lines are never executed. Lines of code that are never executed are a potential source of error, so it’s really good to find out where these are. To do this, we use the program coverage analyzer.

Here’s an example:

 
1>​ cover:start(). %% start the coverage analyser
 
{ok,<0.34.0>}
 
2>​ cover:compile(shout). %% compile shout.erl for coverage
 
{ok,shout}
 
3>​ shout:start(). %% run the program
 
<0.41.0>
 
Playing:<<"title: track018 performer: .. ">>
 
4>​ %% let the program run for a bit
 
4>​ cover:analyse_to_file(shout). %% analyse the results
 
{ok,"shout.COVER.out"} %% this is the results file

The results ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.