MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Author Class-Based Unit Tests in MATLAB


To test a MATLAB program, write a unit test using qualifications that are methods for
testing values and responding to failures.

The Test Class Definition


A test class must inherit from matlab.unittest.TestCase and contain a methods
block with the Test attribute. The methods block contains functions, each of which is a
unit test. A general, basic class definition follows.

%% Test Class Definition
classdef MyComponentTest < matlab.unittest.TestCase

%% Test Method Block
methods (Test)
% includes unit test functions
end
end

The Unit Tests


A unit test is a method that determines the correctness of a unit of software. Each unit
test is contained within a methods block. The function must accept a TestCase instance
as an input.

%% Test Class Definition
classdef MyComponentTest < matlab.unittest.TestCase

%% Test Method Block
methods (Test)

%% Test Function
function testASolution(testCase)
%% Exercise function under test
% act = the value from the function under test

%% Verify using test qualification
% exp = your expected value
% testCase.<qualification method>(act,exp);
end

Author Class-Based Unit Tests in MATLAB
Free download pdf