Write Simple Test Case Using Classes
This example shows how to write a unit test for a MATLAB® function,
quadraticSolver.m.
Create quadraticSolver.m Function
The following MATLAB function solves quadratic equations. Create this function in a
folder on your MATLAB path.
function roots = quadraticSolver(a, b, c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.
if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric')
error('quadraticSolver:InputMustBeNumeric', ...
'Coefficients must be numeric.');
end
roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);
end
Create SolverTest Class Definition
To use the matlab.unittest framework, write MATLAB functions (tests) in the form of
a test case, a class derived from matlab.unittest.TestCase.
Create a subclass, SolverTest.
% Copyright 2015 The MathWorks, Inc.
classdef SolverTest < matlab.unittest.TestCase
methods (Test)
end
Write Simple Test Case Using Classes