This example shows how to write a unit test
for a MATLAB^{®} function, `quadraticSolver.m`

.

**Create quadraticSolver.m Function**

This 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 Function**

Create this function in a folder on your MATLAB path.

function tests = solverTest tests = functiontests(localfunctions); end

A call to `functiontests`

using `localfunctions`

as
input creates an array of tests from each local function in the `solverTest.m`

file.
Each test is a local function that follows the naming convention of
having 'test' at the beginning or end of the function
name. Local functions that do not follow this convention are not included
in the test array. Test functions must accept a single input argument
into which the test framework passes a function test case object.
The function uses this object for verifications, assertions, assumptions,
and fatal assertions. It contains a `TestData`

structure
that allows data to be passed between setup, test, and teardown functions.

**Create Test Function for Real Solutions**

Create a test function, `testRealSolution`

,
to verify that `quadraticSolver`

returns the correct
value for real solutions. For example, the equation `x`

has real solutions ^{2} -
3x + 2 = 0`x = 1`

and ```
x
= 2
```

. This function calls `quadraticSolver`

with
the inputs of this equation. The expected solution, `expSolution`

,
is `[2,1]`

.

Use the qualification function, `verifyEqual`

,
to compare the output of the function, `actSolution`

,
to the desired output, `expSolution`

. If the qualification
fails, the framework continues executing the test. Typically, when
using `verifyEqual`

on floating point values, you
specify a tolerance for the comparison. For more information, see `matlab.unittest.constraints`

.

Add this function to the `solverTest.m`

file.

function testRealSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; verifyEqual(testCase,actSolution,expSolution) end

**Create Test Function for Imaginary Solutions**

Create a test to verify that `quadraticSolver`

returns
the right value for imaginary solutions. For example, the equation `x`

has imaginary solutions ^{2} -
2x + 10 = 0`x = -1 + 3i`

and ```
x
= -1 - 3i
```

. Typically, when using `verifyEqual`

on
floating point values, you specify a tolerance for the comparison.
For more information, see `matlab.unittest.constraints`

.

Add this function, `testImaginarySolution`

,
to the `solverTest.m`

file.

function testImaginarySolution(testCase) actSolution = quadraticSolver(1,2,10); expSolution = [-1+3i -1-3i]; verifyEqual(testCase,actSolution,expSolution) end

The order of the tests within the `solverTest.m`

file
does not matter because they are fully independent test cases.

**Save solverTest Function**

The following is the complete `solverTest.m`

test
file. Save this file in a folder on your MATLAB path.

function tests = solverTest tests = functiontests(localfunctions); end function testRealSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; verifyEqual(testCase,actSolution,expSolution) end function testImaginarySolution(testCase) actSolution = quadraticSolver(1,2,10); expSolution = [-1+3i -1-3i]; verifyEqual(testCase,actSolution,expSolution) end

**Run Tests in solverTest Function**

Run the tests.

`results = runtests('solverTest.m')`

Running solverTest .. Done solverTest __________ results = 1x2 TestResult array with properties: Name Passed Failed Incomplete Duration Totals: 2 Passed, 0 Failed, 0 Incomplete. 0.19172 seconds testing time.

Both of the tests passed.

**Introduce an Error in quadraticSolver.m and Run Tests**

Cause one of the tests to fail by forcing `roots`

in `quadraticSolver.m`

to
be real. Before ending the function, add the line: ```
roots
= real(roots);
```

. (Do not change `solverTest.m`

.)
Save the file and run the tests.

`results = runtests('solverTest.m')`

Running solverTest . ================================================================================ Verification failed in solverTest/testImaginarySolution. --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> Complexity does not match. Actual Complexity: Real Expected Complexity: Complex Actual Value: -1 -1 Expected Value: -1.000000000000000 + 3.000000000000000i -1.000000000000000 - 3.000000000000000i ------------------ Stack Information: ------------------ In C:\work\solverTest.m (testImaginarySolution) at 14 ================================================================================ . Done solverTest __________ Failure Summary: Name Failed Incomplete Reason(s) =============================================================================== solverTest/testImaginarySolution X Failed by verification. results = 1x2 TestResult array with properties: Name Passed Failed Incomplete Duration Totals: 1 Passed, 1 Failed, 0 Incomplete. 0.043751 seconds testing time.

The imaginary test verification failed.

Restore `quadraticSolver.m`

to its previous,
correct version by removing the `roots = real(roots);`

code.

Was this topic helpful?