如何在Matlab中隔离单元测试 [英] How to isolate unittests in Matlab

查看:95
本文介绍了如何在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?

谢谢.

PS:我知道单元测试通常是指测试整个单元,但是我的对象非常复杂并且具有一些非常好的方法,这些方法会被团队不断修改.

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.

推荐答案

对于私有函数,您可以解决可见性规则,以创建函数句柄:

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:

getPrivateFunction.m:

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

对于getPrivateFunction的可能输入,请查看fullfile的文档,允许产生有效路径的所有内容.

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

这篇关于如何在Matlab中隔离单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆