Code covered by the BSD License  

Highlights from
mlunit_2008a

image thumbnail
from mlunit_2008a by Christopher
A MATLAB unit test framework supporting new classdef files (r2008a)

DummyTestCase
classdef DummyTestCase < TestCase
    
% DUMMYTESTCASE This class provides a simple example of how to create a
% test case.

   properties
       my_val;
   end

   methods
       % Use the next two methods when you need to ensure identical initial
       % conditions for each test (e.g., generate test data into a file
       % which will get changed in the test).
       %
       % This is a pretty silly example, but shows how things work (also 
       % see test_setup()).  Note that the test runner calls these for you
       % - you shouldn't call them in your test methods.
       function setUp(self)
           self.my_val = 10;
       end
       
       function tearDown(self)
           self.my_val = [];
       end
       
       % Various trivial tests showing the different assert methods you can
       % call.  You can also provide a specific message to be output in the
       % case of failure (a default message is provided, though.)
       function test_assert_nomsg(self)
           self.assert(true);
       end
       
       function test_assert_msg(self)
           self.assert(false, 'hey, don''t assert false!');
       end
       
       % note that assertEquals assumes (for error reporting purposes) that
       % the first argument is the expected value, and the second argument
       % is the value being tested.
       function test_equals(self)
           self.assertEquals(4, 2+2);
           self.assertEquals(true, true, 'this better work');
           self.assertEquals('hello', 'hello');
           self.assertEquals([4 7 10], 3 + [1 4 7]);
       end
       
       function test_equals_fail(self)
           self.assertEquals(5, 7);
       end
       
       % assertEquals can check equality of structs regardless
       % of the order in which fields are created in the compared
       % structs.  It also works on cell arrays, multidimensional
       % arrays, and objects.          
       function test_deep_equals(self)
           a.foo = 'fruit';
           a.bar.x = 10;
           a.bar.y = {'hey' 19};
           
           b.bar.y = {'hey' 19};
           b.bar.x = 2*5;
           b.foo = lower('FRUIT');
           
           self.assertEquals(a, b, 'this works for nested structs, cell arrays, matrices, etc.');
       end
       
       function test_not_equals(self)
           self.assertNotEquals('x', 65);
       end
       
       % You can define any other functions you want; any functions with
       % names starting with 'test' get run as part of the test.  Anything
       % with a different prefix are not called by the test runner.
       function test_call_other_fns(self)
           self.assert(self.non_test_method());
       end
       
       function tf = non_test_method(self)
           tf = true;
       end
       
       % You can also design your own validations, and call fail() if they
       % fail.
       function test_user_validation(self)
           if true
               self.fail('gosh darn it');
           end
       end
       
       % This shows the behavior of the setUp method (you'll just have to
       % take the tearDown behavior on faith!)
       function test_setup(self)
           self.assertEquals(self.my_val, 10);
       end
       
       % Most exceptions are also handled, and recorded in the test result.
       function test_exception(self)
           k = factorial(4,10);
       end
       
       function test_user_exception(self)
           error('hey, I called error()');
       end
 
   end
end 

Contact us at files@mathworks.com