Class-based Unit Tests: Running Specific Methods
조회 수: 9 (최근 30일)
이전 댓글 표시
I have a simple class:
MyTest < matlab.unittest.TestCase
If I create an instance of this class and use the run method, it will run every test method in MyTest. I want to run only specific test methods. How can I specify which methods to run?
If I pass all of the names to the run method, e.g. testCase.run('test1', 'test2', ...), then the interpreter complains:
Error using matlab.unittest/Test/fromTestCase
Too many input arguments.
Error in matlab.unittest.internal.RunnableTestContent/run (line 47)
suite = Test.fromTestCase(testCase, varargin{:});
Which seems strange, since it seems like this method is meant to handle a variable number of arguments.
댓글 수: 0
채택된 답변
Dave B
2021년 9월 29일
runtests({'foo/test_first','foo/test_second'})
% - or -
runtests("foo","ProcedureName",["test_first" "test_third"])
classdef foo < matlab.unittest.TestCase
methods (Test)
function test_first(testCase)
end
function test_second(testCase)
end
function test_third(testCase)
end
end
end
추가 답변 (1개)
Steven Lord
2021년 9월 30일
While runtests would be my first choice, you could also create a matlab.unittest.TestSuite object and either use a selector in the from* method that builds the suite or use selectIf on a TestSuite you've created earlier. For this class:
classdef example1463594 < matlab.unittest.TestCase
methods(Test, TestTags = {'positive'})
function test_onePlusOne(testcase)
testcase.verifyEqual(1+1, 2);
end
function test_onePlusTwo(testcase)
testcase.verifyEqual(1+2, 3);
end
end
methods(Test, TestTags = {'negative'})
function test_addIncompatibleSizedArrays(testcase)
testcase.verifyError(@() plus([1 2; 3 4], [1 2 3; 4 5 6; 7 8 9]), ...
'MATLAB:dimagree')
end
end
end
To run the whole test class, all three methods:
S = matlab.unittest.TestSuite.fromFile('example1463594.m') % Selects 3 tests
run(S)
To run just the two test methods with TestTags 'positive':
S2 = selectIf(S, 'Tag', 'positive') % Selects 2 tests
{S2.Name}.' % Show which test methods S2 selected
run(S2)
To run just the one test method whose name matches the pattern test_add*:
S3 = selectIf(S, 'ProcedureName', 'test_add*') % Selects 1 test
run(S3)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Write Unit Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!