基于MATLAB类的单元测试:如何传递变量进行测试

时间:2018-08-14 05:03:45

标签: matlab unit-testing

我不确定执行单元测试时如何传递变量。这些变量是通过未置于单元测试中的另一个函数创建的。

方法1:

   classdef myTest < matlab.unittest.TestCase
          properties
            A, B, C
          end
          methods (Test)
            function testDataCoverage(testCase)
              expSol = afunction(A, B, C)
              actSol = 10
              testCase.verifyEqual(testCase, actSol, expSol)
            end
          end
    end

我接下来尝试将变量创建函数(getData)放在单元测试中,但是遇到此错误:

  

混凝土类myTest没有定义名为的TestParameter属性   BdataCoverage方法的新增功能。实现属性或   将类定义为Abstract。

方法2:

classdef myTest < matlab.unittest.TestCase

      properties
      end

      methods (Test)

        function testDataCoverage(testCase)
           [A, B, C] = getData()
           expSol = afunction(A, B, C)
           actSol = 10
           testCase.verifyEqual(testCase, actSol, expSol)
        end

        function [A, B, C] = getData()
            ...code here...
        end

        function Sol = afunction(A, BNew, C)
            ...code here...
        end

      end
end

2 个答案:

答案 0 :(得分:5)

我认为您要使用的是TestParameter

classdef myTest < matlab.unittest.TestCase

  properties (TestParameter)
    param = {1, 2};
  end

  methods (Test)
    function testDataCoverage(testCase, param)
        testCase.verifyEqual(param, 1);
    end
  end
 end

然后,Matlab将自动创建n个测试用例(对于单元格列表中的每个条目),其中参数参数将与TestParameter的另一个条目相关联。因此,您将自动循环所有它们。 (注意:如果每个TestCase包含多个TestParamater,则可能需要研究Matlab文档中的ParameterCombination ...)

这些TestParameter也可以通过(外部)静态方法创建:     classdef myTest

  properties (TestParameter)
    param = myTest.getData();
  end

  methods (Test)
    function testDataCoverage(testCase, param)
        testCase.verifyEqual(param, 1);
    end
  end
  methods (Static)
    function data = getData()
        data = {1,2,3};
    end
  end
 end

Fiy:此外部源仅会触发一次,直到解析该类为止。然后它将停留在内存中,并且matlab缓存该状态。如果您在此处阅读了一些外部配置文件,则可能需要clear all来强制重新创建该类。

子答案:在您的Method2块中,[A, B, C] = getData()缺少自引用myTest.getData()

答案 1 :(得分:1)

MATLAB R2018b刚出门。在此版本中,您现在可以灵活地将测试参数数据注入到基于MATLAB类的测试文件中,而无需修改文件。

数据在MATLAB工作区中的测试文件外部定义。就您而言,您将可以执行以下操作:

classdef myTest< matlab.unittest.TestCase  
  properties (TestParameter)
    A = {1}; % default value for A
    B = {2}; % default value for B
    C = {3}; % default value for C
  end

  methods (Test)
    function testDataCoverage(testCase, A,B,C)
        expSol = afunction(A, B, C);
        actSol = 10;
        testCase.verifyEqual(testCase, actSol, expSol);
    end
  end

end


%Injecting values of A, B and C without modifying the test file
import matlab.unittest.parameters.Parameter;
import matlab.unittest.TestSuite;

>> params = Parameter.fromData( ...
    'A', {11}, ...
    'B', {21},...
    'C', {31}...
);

% Generating suites with the external data
>> suite = TestSuite.fromFile('mytest.m', 'ExternalParameters', params);

% Running test with external data 
>> run(suite)

这里是详细的文档: https://www.mathworks.com/help/matlab/ref/matlab.unittest.testsuite.fromfile.html https://www.mathworks.com/help/matlab/matlab_prog/use-external-parameters-in-parameterized-test.html

相关问题