How to isolate unittests in Matlab

前端 未结 1 1574
长发绾君心
长发绾君心 2021-01-22 19:29

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 mod

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 19:58

    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.

    0 讨论(0)
提交回复
热议问题