zuiqo
zuiqo

Reputation: 1199

How to isolate unittests in Matlab

Given a medium-sized (scientific) codebase, how do you proceed to build a unittest-suite? I need to test local functions as well as hidden methods, but I would prefer not to modify/extend classes so far. Is that possible or do i need to inject testcases somehow? How would I best implement this?

Thanks.

PS: I am aware that commonly unittesting refers to testing entire units, but my objects are quite complex and have some very fancy methods, which are constantly modified by the team.

Upvotes: 2

Views: 124

Answers (1)

Daniel
Daniel

Reputation: 36710

For private functions, you can work around the visibility rules creating a function handle:

%get handle for E:\WORKSPACE\MATLAB\private\object_of_test.m
testfun=getPrivateFunction('E:\WORKSPACE','MATLAB','private','object_of_test.m')
%call function
testfun(pi)

getPrivateFunction.m:

function handle=getPrivateFunction(varargin)
p=fullfile(varargin{:});
[d,f,~]=fileparts(p);
olddir=pwd;
cd(d);
handle=str2func(f);
cd(olddir);
end

For possible inputs of getPrivateFunction please check the documentation for fullfile, everything that results in a valid path is allowed.

Upvotes: 1

Related Questions