This is my first time writing unit tests, so advice from experienced users will be greatly appreciated.
I have several compute functions from which many of them take in the same input arguments, and I would like to write efficient unit tests with minimum code duplication.
I have a following working test class:
classdef ComputeFunction1Test < matlab.unittest.TestCase
methods (Test)
function testFunction1FirstOutput(testCase)
load('test_data.mat')
[act, ~] = computeFunction1(input1, input2, input3);
load('function1ExpectedOut1.mat');
testCase.verifyEqual(act, exp);
end
function testFunction1SecondOutput(testCase)
load('test_data.mat')
[~, act] = computeFunction1(input1, input2, input3);
load('function1ExpectedOut2.mat');
testCase.verifyEqual(act, exp);
end
end
end
I have several more different functions that take the same input arguments and produce output in the same format as computeFunction1.m, which above test class tests. (say computeFunction2.m, computeFunction3.m, and computeFunction3.m, ...)
I could copy the above test class and change the function name to test other functions that take the same input arguments, but this seems inefficient.
Is there a way to handle this situation more efficiently?
Note that I do have other compute functions that require different input arguments as well that need to be tested too. (say specialComputeFunction1 and specialComputeFunction2.m)
2 Comments
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131923
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131923
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131988
Direct link to this comment
https://se.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131988
Sign in to comment.