【问题标题】:Matlab UnitTest CoverageMatlab 单元测试覆盖率
【发布时间】:2013-05-07 17:20:55
【问题描述】:

Matlab 在 2013a 有一个新奇的单元测试框架。我发现它很有帮助,但是随着我的模块的增长,我想知道我已经实现了多少覆盖率。我如何测量我的单元测试覆盖率,类似于 Coverity 等人的方法?

【问题讨论】:

  • IDE 包含一个覆盖工具(已经存在了一段时间)。它是探查器本身。

标签: matlab unit-testing code-coverage


【解决方案1】:

2014b 版提供了plugin 以生成代码覆盖率报告。例如:

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,您将获得刚刚运行的测试的覆盖率报告。

【讨论】:

    【解决方案2】:

    也许我的评论不够清楚。例如,让我们创建一个简单的函数:

    文件夹/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
    

    接下来从“当前文件夹”小部件中,选择“报告 > 覆盖报告”:

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

    点击链接将打开常规的个人资料查看器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2023-04-07
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多