Skip to content

Instantly share code, notes, and snippets.

@chrismullins
Last active December 15, 2015 21:49
Show Gist options
  • Save chrismullins/5328012 to your computer and use it in GitHub Desktop.
Save chrismullins/5328012 to your computer and use it in GitHub Desktop.
Run MATLAB test with xunit
% Once XUnit is installed, you can run this test by navigating to this directory and typing "runtests" into your MATLAB prompt.
%
% Output Variable name should be "test_suite"
% | Make sure the test name either begins or ends with "test"
% | |
% | |
function test_suite = testMyFunctions
initTestSuite;
% Here is the first test!
% This just takes a vector and flips it, and makes sure the fliplr function is working correctly.
% We could easily test any function in the current path, including functions we've written.
% Test should return nothing
% | Again, test starts or ends with "test"
% | |
% | |
function [] = testFliplrVector
in = [1 2 3];
out = fliplr(in);
expected_out = [3 2 1];
if ~isequal(out, expected_out)
error('testFliplrVector:notEqual', 'Incorrect output for vector.')
end
% This is the second test!
% Does the same thing to test a 3x3 matrix input to fliplr.
% The test should return nothing
% | Test should begin or end with "test"
% | |
% | |
function [] = testFliplrMatrix
in = magic(3);
assertEqual(fliplr(in), in(:, [3 2 1]));
% This is the third test!
% Let's test some attributes of the magic matrix.
function [] = testMagicMatrix
in = magic(3);
% All the rows sum to 15.
assertEqual(sum(in), [15 15 15]);
% All the columns sum to 15.
assertEqual(sum(in')', [15 15 15]');
% This is a special magic matrix, because the diagonals sum to 15 as well.
assertEqual(sum(diag(in)), 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment