How to obtain code coverage
조회 수: 6 (최근 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
Pieter
2024년 4월 3일
How can you specify the file location where the code coverage report will be saved?
참고 항목
카테고리
Help Center 및 File Exchange에서 Write Unit Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!