How to obtain code coverage
8 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Simon
2013-10-28
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 个评论
Simon
2013-10-29
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
2021-3-26
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.
MathWorks Support Team
2022-9-27
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Results, Reporting, and Test File Management 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!