Write Simple Test Case Using Classes
This example shows how to write a unit test for a MATLAB® function,
quadraticSolver.m.Create quadraticSolver.m FunctionThe 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.');
endroots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);endCreate SolverTest Class DefinitionTo 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.TestCasemethods (Test)endWrite Simple Test Case Using Classes