How to test custom class with unit test class
조회 수: 17 (최근 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:
- 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.
================================================================================
댓글 수: 0
채택된 답변
Jatin
2024년 9월 9일
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!
댓글 수: 0
추가 답변 (2개)
Deepak Gupta
2024년 9월 9일
편집: Deepak Gupta
2024년 9월 9일
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
Jack
2024년 10월 30일 9:32
It looks like you're facing issues with setting up your unit tests in MATLAB. To create an instance of your custom class in the test class, make sure you initialize it in a method rather than directly in the TestClassSetup. Also, ensure that your test class file is in the same directory or correctly referenced if in a sub-directory. Much like how you might strategize in iq test, a common structure is to have a src/ folder for your classes and a tests/ folder for your test classes. This way, you can easily manage and run your tests. Good luck!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Test Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!