将unittest与自己的Matlab工具箱一起使用 [英] Using unittest with own matlab toolbox
问题描述
我在matlab中继承了一个代码库,我希望将该代码库与matlab.unittest框架一起进行单元测试.
I inherited a code base in matlab, which I like to put under unittest with the matlab.unittest framework.
为了使代码库对用户的任意addpath更加健壮,我将大多数代码放入了类似于工具箱的+文件夹中.因此,总体布局为:
To make the code base more robust against arbitrary addpath of my users, I have put most of the code into +folders like a toolbox. So the general layout is:
+folder1/file1.m
+folder1/runtestsuite.m
+folder1/unittest_data/file1_testdata.mat
+folder1/+folder2/file2.m
+folder1/+folder2/unittest_data/file2_testdata.mat
...
并使用正确的导入语句更新所有内部引用.
and updated all internal references with the correct import statements.
现在,我想为file1.m添加一个单元测试.但是,如果我将文件放在+ folder1/file1_test.m中,则file1.m似乎不可见.
Now, I like to add a unittest for file1.m. However if I put a file in +folder1/file1_test.m file1.m seems not to be visible.
这是我的file1_test.m示例代码
Here is my example code of file1_test.m
classdef file1_test < matlab.unittest.TestCase
properties
path
end
methods(TestMethodSetup)
function setunittestdatapath(testCase)
p = mfilename('fullpath');
[directory,~,~]=fileparts(p);
testCase.path = fullfile(directory,'unittest_data');
end
end
methods (Test)
function file1_input(testCase)
%import folder1.file1
testdata = load(fullfile(testCase.path),'file1_testdata.mat');
result = file1(testdata.input);
testCase.verifyEqual(result, testdata.output);
end
end
end
如果我取消对import语句的注释,则unittest可以正常工作.因此,目前我必须将所有导入语句添加到每个测试中,这是我想避免的.有没有一种更优雅的方式来做这样的事情?
If I uncomment the import statement the unittest works fine. So currently I have to add all import statements to each individual test, which I like to avoid. Is there a more elegant way for doing something like this?
我尝试在文件的开头导入它,尽管matlab抱怨在CLASSDEF解析错误:用法可能是无效的MATLAB语法".这也有效.那么做这样的事情的正确,最务实的方法是什么?
I tried importing it at the beginning of the file, although matlab complains "Parse error at CLASSDEF: usage might be invalid MATLAB syntax." this also works. So what is the correct and most pragmatically way for doing something like this?
推荐答案
import
语句仅适用于使用它们的本地范围,因此,如果您希望某个函数不能使用全限定名,那么您必须将import
语句分别添加到每个函数中.
import
statements only apply to the local scope of where they are used so if you want a function to be able to not use the full-qualified name, then you'll have to add the import
statement to each function separately.
导入列表范围定义如下:
The import list scope is defined as follows:
-
从MATLAB®命令提示符调用的
-
脚本-范围是基本的MATLAB工作区.
Script invoked from the MATLAB® command prompt — Scope is the base MATLAB workspace.
函数,包括嵌套函数和局部函数-作用域是该函数,并且该函数不共享父函数的导入列表.如果在MATLAB函数或脚本以及任何本地函数中需要导入列表,则必须为每个函数调用导入函数.
Function, including nested and local function — Scope is the function and the function does not share the import list of the parent function. If the import list is needed in a MATLAB function or script and in any local functions, you must call the import function for each function.
但是对于单元测试,我认为最好是每次都使用完全合格的函数名(而不是依赖于import
),以便用户清楚所测试的内容.
For unit tests though, I would argue that it is probably best to use the fully-qualified function name every time (rather than relying on import
) so that it's clear to the user what you're testing.
result = folder1.file1(testdata.input)
这篇关于将unittest与自己的Matlab工具箱一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!