Matlab单元测试覆盖率

5

Matlab 2013a引入了一个新的单元测试框架。虽然我发现它很有帮助,但随着我的模块越来越多,我想知道我已经实现了多少覆盖率。我该如何测量我的单元测试覆盖率,类似于coverity等工具?


1
该集成开发环境(IDE)已经包含了覆盖率工具(已经使用了相当长的时间)。它就是分析器本身。 - Amro
2个回答

10

2014b版本提供了一个用于生成代码覆盖报告的插件。例如:

import matlab.unittest.TestRunner;
import matlab.unittest.TestSuite;
import matlab.unittest.plugins.CodeCoveragePlugin;

% Create a TestSuite array
suite = TestSuite.fromFolder(testFolder);

% Create a runner and add the code coverage plugin
runner = TestRunner.withTextOutput;
runner.addPlugin(CodeCoveragePlugin.forFolder(sourceFolder));

% Run the suite. This opens a code coverage report when done testing.
result = runner.run(suite)

请注意,测试覆盖率报告应该在源代码上运行,而测试套件是从一个单独的文件夹生成的。如果你像链接的例子中一样使用pwd,那么你将获得刚刚运行的测试的覆盖率报告。


8
也许我的评论没有表达清楚。例如,让我们创建一个简单的函数:

folder/test1.m

x = zeros(100,1);
for i=1:100
    if rand < 0.8
        x(i) = 1;
    else
        x(i) = 2;
    end
end

现在运行并分析脚本:
>> profile on
>> test1
>> profile off

从“当前文件夹”小部件中选择“报告 > 覆盖率报告”:

coverage_report

这将为当前文件夹中的所有函数/脚本提供覆盖率报告:

report

点击链接将打开常规配置文件查看器:

prof_viewer

显然,您也可以直接从每个文件的配置文件查看器中选择上述选项...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接