Matlab Unit test for structure input. How to use same test for n numbers.

조회 수: 13 (최근 30일)
  1. I have a matlab unit test class. Here i am using setup method to form structure i.e. 'testCase.out' this structure have n values here n=4.
  2. Test method is used to verify equality of values.
classdef ClassTest < matlab.unittest.TestCase
properties (Access = private)
out
end
methods (TestClassSetup)
function FirstSetup(testCase)
n=4;
for i=1:n
testCase.out(i).name= ['Test_', num2str(i)];
testCase.out(i).value = i;
end
end
end
methods (Test)
function MyTest(testCase)
for k=1:length(testCase.out)
Test = testCase.out(k).name;
testCase.verifyEqual(testCase.out(k).value,2);
end
end
end
end
Now to run this test:
import matlab.unittest.TestSuite
suite = TestSuite.fromClass(?ClassTest);
result = run(suite);
The result is failed as k=1,3 and 4 is not equal values. Only k=2 value is equal.
Question:
How could we create a test such that for value 2 the test will pass and for other test will fail. So i want result table i.e. Test1 = fail, Test2=Pass, Test3=fail and Test4 = fail.
how could we use above same test and get result table for n numbers with pass fail status?

채택된 답변

Bharathi Sunkara
Bharathi Sunkara 2020년 11월 5일
Hi Sudhakar,
As per my understanding, you can make use of the “TestParameter” property available in MATLAB in order to create the structure of values and use the structure in the verifyEqual, summary of the test is displayed in the table at the end of the test result.
Here is how you can do:
classdef ClassTest < matlab.unittest.TestCase
properties (Access = private)
out
end
properties (TestParameter)
testValue={1,2,3,4}
end
methods (Test)
function MyTest(testCase, testValue)
testCase.verifyEqual(testValue,2);
end
end
end
Hope this Helps!
  댓글 수: 1
Sudhakar Shinde
Sudhakar Shinde 2020년 11월 5일
Thanks Bharathi. I understood the way of using TestParameter property. But i am expecting the structure formation should happen in setup method and then i can use this structure to pass in test method.
parameterized test works good if we form a structure using TestParameter property.
How can we form structure in setup method and then i can use parameterized test to verifyEqual.?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 11월 4일
It sounds like you want to create a parameterized test.
  댓글 수: 1
Sudhakar Shinde
Sudhakar Shinde 2020년 11월 5일
Thanks steven. I already gone through this page but still haven't achieved. It may take more time to understand parameterized test. meanwhilec can you please help me to have above simple example.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by