Pass a function as a property to a Test class

조회 수: 1 (최근 30일)
Neelabh Jyoti Saharia
Neelabh Jyoti Saharia 2022년 11월 4일
답변: Guru Kumaresan 2022년 11월 7일
Hi, I am new to the unit test framework. I am writing a simple test class with two test methods as shown below.
classdef Tester_Class < matlab.unittest.TestCase
properties
num
expSolution1
expSolution2
end
methods(Test)
function squareTest(testCase, num, expSolution1)
[actSolution, ~] = exampleFunction(num);
testCase.verifyEqual(actSolution,expSolution1)
end
function rootTest(testCase, num, expSolution2)
[~, actSolution] = exampleFunction(num);
testCase.verifyEqual(actSolution,expSolution2)
end
end
end
I want the two test methods to be able to test the function exampleFunction that gives two output (square of the number and root of the number). I want to pass the number on which the function under test will work on, and the two expected output, like
Tester = Tester_Class('num', 9, 'expSolution1', 81, 'expSolution2', 3)
How can I do this?

답변 (1개)

Guru Kumaresan
Guru Kumaresan 2022년 11월 7일
Hi Neelabh,
From my understanding, you are trying to pass external parameters that are not defined in the test file itself. This can be done by creating an array of Parameter instances, and then use the ExternalParameters name-value argument with TestSuite creation methods such as fromClass.
You can also refer to MathWorks Documentation page for Using External Parameters in Parameterized functions.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Write Unit Tests에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by