Quick Start

mlUnit: MATLAB Unit Testing

Jump to: navigation, search


What Is mlUnit?

mlUnit is a unit testing framework for the MATLAB .m language, considering the patterns of the xUnit family. Expressed in other words, mlUnit is the MATLAB .m language version of JUnit.

mlUnit and all associated files are released unter the GNU General Public License (GPL) as published by the Free Software Foundation.

Installation

Download and unzip the zip file and add the directories to the MATLAB path:

addpath('$MLUNIT\src');
addpath('$MLUNIT\test');

Replace $MLUNIT with the root mlUnit directory.

How To Test?

As an example a test for the built-in sin function is written:

  1. Create a new directory @test_sin:
    mkdir @test_sin
    cd @test_sin
  2. Create a new .m file test_sin.m (the constructor):
    edit test_sin.m
  3. The test case test_sin is inherited from the class test_case, so add the following lines to test_sin.m:
    function self = test_sin(name)
    
    tc = test_case(name);
    self = class(struct([]), 'test_sin', tc);
  4. The first test should check, that sin(0) = 0. Therefore create a new file test_null.m (the first test) and add the following lines:
    function self = test_null(self)
    
    assert_equals(0, sin(0));
  5. Run the test:
    cd ..
    runner = text_test_runner(1, 1);
    loader = test_loader;
    run(runner, load_tests_from_test_case(loader, 'test_sin'));
  6. You should see something like this:
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.010s
    
    OK
  7. Add more tests, e.g. test_sin_cos.m, which checks the correlation of cos and sin:
    function self = test_sin_cos(self)
    
    assert_equals(cos(0), sin(pi/2));
  8. Run the tests:
    run(runner, load_tests_from_test_case(loader, 'test_sin'));
  9. You should see something like this:
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 0.010s
    
    OK
  10. Try other parameters of text_test_runner:
    runner = text_test_runner(1, 2);
    run(runner, load_tests_from_test_case(loader, 'test_sin'));</pre>
    
  11. You should see something like this:
    test_null(test_sin) ... OK
    test_sin_cos(test_sin) ... OK
    
    ----------------------------------------------------------------------
    Ran 2 tests in 0.020s
    
    OK
  12. Read more in the help texts of the classes, e.g.:
    help text_test_runner
    

mlUnit Tests

As mlUnit was developed test-driven, there are a number of tests in the test directory, which can be run by
test_mlunit;

Question, Comments, Bugs

If you have a question, a comment or a bug report, please send me an email. Usually I try to answer within 24 hours. Spam and other crap goes automatically to the trash bin, so please use a precise subject.

Navigation