How to test custom class with unit test class
Since, I’am very new to the topic of MatLab unit tests, I struggle with testing a custom MatLab class using a unit test class in a separate file.
Like I know from other programming languages, I would like to write a unit test (in a separate class) for a custom MatLab class. However, I do not see how to create an instance of / how to use my custom class within the test class.
Therefore, I would have the following questions, where I hope you can help me:
How do I properly create an instance of my custom class within the test class?
When I place my test class file in a sub-directory "tests/", how do I include the file of the my custom class?
Can you recommend a proper folder structure? Concrete, is it appropriate to place test classes in a sub-folder tests/
Thank you very much!
PS: Up to now – as minimal example – I have a file with a custom class MyMath.m:
classdef MyMath
methods
function obj = MyMath()
end
function outputArg = add(~, a1, a2)
outputArg = a1+a2;
end
end
end
and a file with the test class MyMath_Test.m
classdef MyMath_Test < matlab.unittest.TestCase
methods(TestClassSetup)
% Shared setup for the entire test class
my_math = MyMath();
end
methods(TestMethodSetup)
% Setup for each test
end
methods(Test)
% Test methods
function unimplementedTest(testCase)
res = my_math.add(4,5);
testCase.verifyEqual(res, 9)
end
end
end
Running Tests via Matlab GUI Buttons, results in the error:
>> runtests("MyMath_Test")
Running MyMath_Test
================================================================================
Error occurred while setting up or tearing down MyMath_Test.
As a result, all MyMath_Test tests failed and did not run to completion.
———
Error ID:
———
‘MATLAB:TooManyInputs’
————–
Error Details:
————–
Error using MyMath_Test/MyMath
Too many input arguments.
================================================================================Since, I’am very new to the topic of MatLab unit tests, I struggle with testing a custom MatLab class using a unit test class in a separate file.
Like I know from other programming languages, I would like to write a unit test (in a separate class) for a custom MatLab class. However, I do not see how to create an instance of / how to use my custom class within the test class.
Therefore, I would have the following questions, where I hope you can help me:
How do I properly create an instance of my custom class within the test class?
When I place my test class file in a sub-directory "tests/", how do I include the file of the my custom class?
Can you recommend a proper folder structure? Concrete, is it appropriate to place test classes in a sub-folder tests/
Thank you very much!
PS: Up to now – as minimal example – I have a file with a custom class MyMath.m:
classdef MyMath
methods
function obj = MyMath()
end
function outputArg = add(~, a1, a2)
outputArg = a1+a2;
end
end
end
and a file with the test class MyMath_Test.m
classdef MyMath_Test < matlab.unittest.TestCase
methods(TestClassSetup)
% Shared setup for the entire test class
my_math = MyMath();
end
methods(TestMethodSetup)
% Setup for each test
end
methods(Test)
% Test methods
function unimplementedTest(testCase)
res = my_math.add(4,5);
testCase.verifyEqual(res, 9)
end
end
end
Running Tests via Matlab GUI Buttons, results in the error:
>> runtests("MyMath_Test")
Running MyMath_Test
================================================================================
Error occurred while setting up or tearing down MyMath_Test.
As a result, all MyMath_Test tests failed and did not run to completion.
———
Error ID:
———
‘MATLAB:TooManyInputs’
————–
Error Details:
————–
Error using MyMath_Test/MyMath
Too many input arguments.
================================================================================ Since, I’am very new to the topic of MatLab unit tests, I struggle with testing a custom MatLab class using a unit test class in a separate file.
Like I know from other programming languages, I would like to write a unit test (in a separate class) for a custom MatLab class. However, I do not see how to create an instance of / how to use my custom class within the test class.
Therefore, I would have the following questions, where I hope you can help me:
How do I properly create an instance of my custom class within the test class?
When I place my test class file in a sub-directory "tests/", how do I include the file of the my custom class?
Can you recommend a proper folder structure? Concrete, is it appropriate to place test classes in a sub-folder tests/
Thank you very much!
PS: Up to now – as minimal example – I have a file with a custom class MyMath.m:
classdef MyMath
methods
function obj = MyMath()
end
function outputArg = add(~, a1, a2)
outputArg = a1+a2;
end
end
end
and a file with the test class MyMath_Test.m
classdef MyMath_Test < matlab.unittest.TestCase
methods(TestClassSetup)
% Shared setup for the entire test class
my_math = MyMath();
end
methods(TestMethodSetup)
% Setup for each test
end
methods(Test)
% Test methods
function unimplementedTest(testCase)
res = my_math.add(4,5);
testCase.verifyEqual(res, 9)
end
end
end
Running Tests via Matlab GUI Buttons, results in the error:
>> runtests("MyMath_Test")
Running MyMath_Test
================================================================================
Error occurred while setting up or tearing down MyMath_Test.
As a result, all MyMath_Test tests failed and did not run to completion.
———
Error ID:
———
‘MATLAB:TooManyInputs’
————–
Error Details:
————–
Error using MyMath_Test/MyMath
Too many input arguments.
================================================================================ unit test, object oriented test MATLAB Answers — New Questions