How to obtain code coverage

7 views (last 30 days)
SANTOSH
SANTOSH on 28 Oct 2013
Commented: Pieter on 3 Apr 2024
Hi. I'm working on code that has one main function and seven local functions. I need to measure the coverage of this code using five test cases. In other words, I want to see how much of the code is covered by the tests when they run. Please share your ideas. Thanks in advance.

Answers (2)

Simon
Simon on 28 Oct 2013
Hi!
Do you know the profiler? This is what you need. Look in the documentation on how to profile a script/function. This gives you many information about the execution. For your question: look at the resulting "ExecutedLines" array.
You can profile the function for each test case and get the aggregated "ExecutedLines" for all test cases. Compare that to the real code lines (non-blank and non-comment lines).
  3 Comments
Simon
Simon on 29 Oct 2013
Hi!
To explain my thought in detail, suppose we have a test file calling 2 functions to test. The functions are funtest.m and funtest2.m
.m
function a = funtest(b)
if b==1
a=1;
else
a=0;
end
end
funtest2.m
function a2 = funtest2(b2)
if b2==1
a2=2;
else
a2=0;
end
end
The test file is
% start profiling
profile on
% call functions with test case 1
in=1;
funtest(in)
funtest2(in)
% call functions with test case 2
in=2;
funtest(in)
funtest2(in)
% get profile results
p = profile('info');
% compute coverage
Coverage = zeros(length(p.FunctionTable), 1);
for n = 1:length(p.FunctionTable)
% executed lines in functions
ExecutedLines = p.FunctionTable(n).ExecutedLines;
% runnable lines (undocumented matlab feature!)
RunnableLines = callstats('file_lines', p.FunctionTable(n).CompleteName);
% coverage in percent
Coverage(n) = 100 * length(unique(ExecutedLines(:, 1))) / ...
length(unique(RunnableLines));
end
If you, for example, comment out the second test case you will get a coverage of 50 percent. Try it out!
Jared Rize
Jared Rize on 26 Mar 2021
This is a great answer; however, how can you determine the total number of lines for a sub-function? For instance, if you had a script that you were checking code coverage on that was broken down in func1() and func2(), I believe you could get the number of executed lines for those functions but not the number of lines in each function.

Sign in to comment.


MathWorks Support Team
MathWorks Support Team on 27 Sep 2022
To generate a code coverage report for your MATLAB source code while running tests, specify the source code by using the ReportCoverageFor name-value argument of the runtests function.
For example, assume that your main function and local functions are defined in a file named myFunc.m in your current folder. Also, assume that your test cases are defined in a file named myTests.m in your current folder. This code shows how to run your tests and generate an HTML code coverage report for the source code in myFunc.m.
runtests("myTests.m","ReportCoverageFor","myFunc.m")
You also can generate a code coverage report by adding an instance of the CodeCoveragePlugin class to a test runner. For more information, see Generate Code Coverage Report in HTML Format.
  1 Comment
Pieter
Pieter on 3 Apr 2024
How can you specify the file location where the code coverage report will be saved?

Sign in to comment.

Categories

Find more on Results, Reporting, and Test File Management in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!