首页 > 解决方案 > Matlab 单元测试在通过和失败之间交替(通过“奇数”运行,“偶数”失败)

问题描述

我对代码进行了一些单元测试,这些代码正在执行一些非常小的图像处理(将几个小图像组合成一个更大的图像)。当我运行测试时,我注意到其中四分之三在他们从目录中读取图像的那一行失败(失败并出现索引越界错误)。

但是,如果我再次运行它,它们都会通过。当我也在编写代码时,我注意到每当我在代码中设置断点时,我都必须运行两次单元测试,因为(在第一次之后)它会运行测试而不会遇到任何断点。

我的仓库是这样组织的:

src/
    /* source code .m files are in here */

unit_tests/
    images/
        squares/
            - img1.png
            - img2.png
            ...
            - imgn.png
    - unit_tests.m

我的设置中有一行(在 unit_tests.m 中)为所有代码生成和添加路径:

function tests = unit_tests()
    addpath(genpath('..'));    
    tests = functiontests(localfunctions);
end

单元测试都具有这种格式:

function testCompositeImage_2x3(testCase)
    squares = dir('images/squares/*.png');
    num_images = length(squares);
    img = imread([squares(1).folder filesep squares(1).name]); % all same size squares
    rows = 2;
    cols = 3;
    buffer = 2;

    for idx = 1:num_images - (rows*cols)
        imarray = cell(1,(rows*cols));
        n = 1;
        for ii = idx:idx +(rows*cols) -1
            imarray{n} = imread([squares(ii).folder filesep squares(ii).name]);
            n = n + 1;
        end

        newimg = createCompositeImage(rows,cols,imarray, buffer);

        expCols = cols*size(img,1) + (cols+1)*2*buffer;
        expRows = rows*size(img,2) + (rows+1)*2*buffer;
        assert(checksize(newimg, expRows, expCols, 3) == true);
    end
end

(“checksize”只是我写的一个帮助器,它返回一个布尔 b/c 断言不比较矩阵)

当我启动一个新的 matlab 会话并运行单元测试(使用编辑器选项卡中的“运行测试”按钮)时,它们会通过以下输出:

>> runtests('unit_tests\unit_tests.m')
Running unit_tests
.......
Done unit_tests
__________


ans = 

  1×7 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   7 Passed, 0 Failed, 0 Incomplete.
   0.49467 seconds testing time.

再次运行它(再次,通过按下按钮):

>> runtests('unit_tests')
Running unit_tests
..
================================================================================
Error occurred in unit_tests/testCompositeImage_2x2 and it did not run to completion.

    ---------
    Error ID:
    ---------
    'MATLAB:badsubscript'

    --------------
    Error Details:
    --------------
    Index exceeds array bounds.

    Error in unit_tests>testCompositeImage_2x2 (line 47)
        img = imread([squares(1).folder filesep squares(1).name]); % all same size
================================================================================
/*similar error info for the other two failing tests...*/
...
Done unit_tests
__________

Failure Summary:

     Name                               Failed  Incomplete  Reason(s)
    ==================================================================
     unit_tests/testCompositeImage_2x2    X         X       Errored.
    ------------------------------------------------------------------
     unit_tests/testCompositeImage_2x3    X         X       Errored.
    ------------------------------------------------------------------
     unit_tests/testCompositeImage_3x2    X         X       Errored.


ans = 

  1×7 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   4 Passed, 3 Failed (rerun), 3 Incomplete.
   0.0072287 seconds testing time.

它基本上在第一行失败了,因为它没有从文件夹中读取任何内容,这让我怀疑即使其他 4 个测试应该通过,它们实际上根本没有运行。然而,如果我再次运行测试,它们都会通过。第四次运行它,它们再次失败。

起初,我认为单元测试可能执行得太快(仅在偶数运行时?)并且它在设置中的 addpath/genpath 函数完成之前运行单元测试,所以我添加了一个暂停语句和重新运行测试,但我遇到了同样的问题,只是这次它会等待必要的秒数,然后再继续并失败。如果我再次运行它,没问题 - 我所有的测试都通过了。

我完全不知道为什么会这样;我正在使用在 Win10 机器上运行的 vanilla matlab (R2018a),并且没有任何花哨的东西。我觉得您应该能够根据需要多次运行单元测试并期望得到相同的结果!有什么我刚刚忽略的吗?或者这是一些奇怪的功能?

标签: matlabunit-testingalternating

解决方案


添加我的修复以防其他人遇到相同的问题。

正如克里斯所指出的,关于这条线的一些事情

addpath(genpath('..'));

导致 GUI 进入一个奇怪的状态,按下“运行测试”按钮在调用之间交替runtests('unit_tests\unit_tests.m')runtests('unit_tests')这反过来又导致测试交替通过和失败。路径变量本身似乎不是问题(因为它总是包含 - 至少 - 必要的目录),而是 matlab 本身固有的问题。我最接近问题根源的是对dir函数内部(编译)函数的genpath调用。

“正确”的解决方案是从 unit_tests 函数中完全删除该行并将其添加到 setupOnce 函数中:

function tests = unit_tests()
    tests = functiontests(localfunctions);
end

function setupOnce(testCase)
    addpath(genpath('..'));
end

不需要 setupOnce 函数的 hack(不推荐)如下:

function tests = unit_tests()
    pth = fullfile(fileparts(fileparts(mfilename('fullpath'))),'src');
    paths = regexp(genpath(pth), ';', 'split');
    for idx = 1:length(paths) - 1 % last element is empty
        addpath(paths{idx});
    end
end

我需要重新启动 matlab 才能使更改生效。这适用于我使用在 Win10 上运行的 r2018a 进行的设置。


推荐阅读