Code covered by the BSD License  

Highlights from
MATLAB xUnit Test Framework

image thumbnail

MATLAB xUnit Test Framework

by

 

31 Jan 2009 (Updated )

MATLAB xUnit is a unit test framework for MATLAB code.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

MATLAB xUnit Test Framework: How to Put Multiple Test Cases in One M-file

MATLAB xUnit Test Framework: How to Put Multiple Test Cases in One M-file

The Quick Start example showed how you can write a simple M-file to be a single test case. This example shows you how to put multiple test cases in one M-file.

Name your M-file beginning or ending with "test", like "testMyFunc". Start by putting the following two lines at the beginning of the file. It's important that the output variable name on line 1 be test_suite.

  function test_suite = testMyFunc
  initTestSuite;

Next, add subfunctions to the file. Each subfunction beginning or ending with "test" becomes an individual test case.

The directory example_subfunction_tests contains a test M-file containing subfunction test cases for the fliplr function.

cd example_subfunction_tests

type testFliplr
function test_suite = testFliplr
initTestSuite;

function testFliplrMatrix
in = magic(3);
assertEqual(fliplr(in), in(:, [3 2 1]));

function testFliplrVector
assertEqual(fliplr([1 4 10]), [10 4 1]);


As usual, run the test cases using runtests:

runtests
Starting test run with 2 test cases.
..
PASSED in 0.027 seconds.

Back to MATLAB xUnit Test Framework

Contact us