'Use unit-testing framework in matlab to test data

I would like to test datasets with a variable number of values. Each value should be tested and I would like to have a standardized output that I can read in afterward again. My used framework is Matlab.

Example:
The use case would be a dataset which includes, e.g., 14 values that need to be testet. The comparison is already completely handled by my implementation. So I have 14 values, which I would like to compare against some tolerance or similar and get an output like

1..14
ok value1
ok value2
not ok value3
...
ok value14

Current solution:
I try to use the unit-testing framework and the according TAPPlugin that would produce exactly such an output (tap), one for every unit-test. My main problem is that the unit-testing framework does not take any input parameters. I already read about parametrization, but I do not know how this helps me. I could put the values as a list into the parameter, but how to I pass them there? Afaik the unit-test class does not allow additional parameters during initialization, so I cannot include this in the program the way I want.

I would like to avoid to need to format the TAP output on my own, because it is already there, but only for unit-test objects. Unfortunately, I cannot see how to implement this wisely.

How can I implement the output of a test anything protocol where I have a variable amount of comparisons (values) in Matlab?



Solution 1:[1]

If you are using class based unit tests you could access its properties from outside the test.

So let's say you have following unit test:

classdef MyTestCase < matlab.unittest.TestCase

properties
    property1 = false;
end

methods(Test) 
    function test1(testCase)
       verifyTrue(testCase,testCase.property1) 
    end
end

You could access and change properties from outside:

test=MyTestCase; 
MyTestCase.property1 = true;
MyTestCase.run; 

This should no succeed since you changed from false to true. If you want to have a more flexible way, you could have a list with the variables and a list with the requirements and then cycle through both in one of the test functions.

properties
    variables = [];
    requirements = [];
end

methods(Test) 
   function test1(testCase)
      for i = 1:length(variables):
         verifyEqual(testCase,testCase.variables[i],testCase.requirements [i]) 
      end
   end
end

Now you would set variables and requirements:

test=MyTestCase; 
MyTestCase.variables = [1,2,3,4,5,6];
MyTestCase.requirements = [1,3,4,5,5,6];
MyTestCase.run; 

Please note, that in theory you should not have multiple assert statements in one test.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1