MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);

testName = pluginData.Name;
testCase.addlistener('AssertionFailed', ...
@(~,event)plugin.recordData(event,testName, 'Assertion'));
testCase.addlistener('FatalAssertionFailed', ...
@(~,event)plugin.recordData(event,testName, 'Fatal Assertion'));
testCase.addlistener('VerificationFailed', ...
@(~,event)plugin.recordData(event,testName, 'Verification'));
end
end

methods (Access = private)
function recordData(plugin,eventData,name,failureType)
s.Name = {name};
s.Type = {failureType};
if isempty(eventData.TestDiagnosticResult)
s.TestDiagnostics = 'TestDiagnostics not provided';
else
s.TestDiagnostics = eventData.TestDiagnosticResult;
end
s.FrameworkDiagnostics = eventData.FrameworkDiagnosticResult;
s.Stack = eventData.Stack;
s.Timestamp = datetime;

plugin.FailedTestData = [plugin.FailedTestData; struct2table(s)];
end
end
end

Create Test Class

In your working folder, create the file ExampleTest.m containing the following test
class.

classdef ExampleTest < matlab.unittest.TestCase
methods(Test)
function testOne(testCase)
testCase.assertGreaterThan(5,10)
end
function testTwo(testCase)
wrongAnswer = 'wrong';
testCase.verifyEmpty(wrongAnswer,'Not Empty')
testCase.verifyClass(wrongAnswer,'double','Not double')
end

33 Unit Testing

Free download pdf