How to test custom class with unit test class

52 次查看(过去 30 天)
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:
  1. How do I properly create an instance of my custom class within the test class?
  2. When I place my test class file in a sub-directory "tests/", how do I include the file of the my custom class?
  3. 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.
================================================================================

采纳的回答

Jatin
Jatin 2024-9-9,11:08
To correctly create an instance of your custom class within the test class, define a property in the test class that is accessible to all test methods. This can be achieved using the "properties" block.
If your test class is located in a subdirectory, ensure that directory is included in MATLAB's path by using the "Add to Path" option.
Organizing your test cases in a subdirectory named "tests/" is a good practice, as it helps keep your code well-organized.
Here is a correct example of a unit test class for the custom class you mentioned.
classdef MyMath
methods
function obj = MyMath()
end
function outputArg = add(~, a1, a2)
outputArg = a1 + a2;
end
end
end
Test class:
classdef MyMath_Test < matlab.unittest.TestCase
properties
myMath % Property to hold an instance of MyMath
end
methods(TestMethodSetup)
function createMyMathInstance(testCase)
% Setup for each test - create an instance of MyMath
testCase.myMath = MyMath();
end
end
methods(Test)
function testAddMethod(testCase)
% Test the add method
res = testCase.myMath.add(4, 5);
testCase.verifyEqual(res, 9);
end
end
end
Kindly go through the below documentation for more on "Ways to right unit tests":
Hope this helps!

更多回答(1 个)

Deepak Gupta
Deepak Gupta 2024-9-9,11:27
编辑:Deepak Gupta 2024-9-9,11:28
Hi,
Methods keyword is used to define a wrapper for all the method which need to run during class setup or method setup or test themselve, just like you have done for methods(Test) . Here is updated code, with few changes.
classdef MyMath_Test < matlab.unittest.TestCase
properties
my_math % You have defined your class object in a TestClassSetup. It needs to be declared first
% as a property I would rather suggest you to create class object
% inside test method or TestMethodSetup so object gets created for
% each run individually.
end
methods(TestClassSetup)
% To answer your question, regarding where to place tests. You can
% place them anywhere. I would suggest you create a separate
% directly for tests from source. You can use path fixture to add
% source path for your test to use at runtime. Any function defined
% as TestClassSetup will only run once at the start of this test.
function pathFixture(testCase)
import matlab.unittest.fixtures.PathFixture
testCase.applyFixture(PathFixture("SourcePath\Custom_Class"))
end
end
methods(TestMethodSetup)
% Any function defined as TestMethodSetup will run for each test
% point and will create and destruct objects automatically after
% test point is complete
function CreateClassObject(testCase)
testCase.my_math = MyMath();
end
end
methods(Test)
% Test methods
function unimplementedTest(testCase)
res = testCase.my_math.add(4,5);
testCase.verifyEqual(res, 9)
end
end
end
If you create object inside test method itself it will be like this:
classdef MyMath_Test2 < matlab.unittest.TestCase
methods(TestClassSetup)
function pathFixture(testCase)
import matlab.unittest.fixtures.PathFixture
testCase.applyFixture(PathFixture("SourcePath\Custom_Class"))
end
end
methods(Test)
% Test methods
function unimplementedTest(testCase)
my_math = MyMath();
res = my_math.add(4,5);
testCase.verifyEqual(res, 9)
end
end
end
Cheers,
Deepak
  1 个评论
Christian
Christian 2024-9-9,13:12
thank you very much for your fast answer and all the interesting background/additional information.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Test Scripts 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by