How to perform multiple identical unit tests.

조회 수: 19 (최근 30일)
Graham House
Graham House 2013년 11월 28일
댓글: Matlab Pro 2023년 12월 25일
Apologies if my question has a relativity simple solution which I have failed to see, but I'm really quite new to the MATLAB unit test framework.
Basically I have a list of over 100 MATLAB scripts that I need to perform the same unit test on. Is there an easy way to do this without having to write out each test individually?
I should also point out that it's important I'm able to see which .m file each test refers to.
Thanks

채택된 답변

David Hruska
David Hruska 2013년 11월 30일
Hi Graham,
One approach is to define your test content in an abstract base class. The test content would refer to an abstract property or method in the base class that is implemented by each subclass. You would need 100+ subclasses (one for each of the scripts that you want to test). Andy Campbell gives a concrete example of this approach here. This approach has the obvious disadvantage of requiring the definition of over 100 test classes; however, they would likely be quite simple. One benefit is that you can easily run the unit test for just one script file by itself.
Alternatively, you could consider writing Test methods that operate on all the scripts using a for loop. In order to provide defect localization in the event of a test failure, you could construct a diagnostic that identifies the script being tested. Here's a rough outline of an example:
classdef exampleTest < matlab.unittest.TestCase
methods (Test)
function test1(testCase)
% Test 100 scripts
for i = 1:100
% Operate on script i
testCase.verifyThat(result_of_script_i, <constraint>, ...
sprintf('Testing script %d', i));
end
end
end
end
With this approach, you lose the ability to easily run the test for just one of the scripts by itself, but the test is fairly easy to write.
Hope this helps,
David

추가 답변 (1개)

David Hruska
David Hruska 2014년 3월 7일
Release 2014a provides another solution that you may want to consider -- writing a parameterized test. Please see this answer for more details.
  댓글 수: 2
Matlab Pro
Matlab Pro 2023년 12월 25일
Here is a specific example that can help you:
This examples calls get_files_to_analyse(), a tests is automatically defined for each file
Yuu do not need to change any of the files
classdef exampleTest < matlab.unittest.TestCase
properties (TestParameter)
files = get_files_to_analyse();
end
methods (Test)
function test1(testCase, files)
results = operate_on_file(files);
testCase.verifyThat(results, <constraint>, ...
sprintf('Testing script %d', i));
end
end
end
%-------------------------------------------------------------
function files = get_files_to_analyse()
base_path_of_files_to_analyse = pwd; % base_path_of_files_to_analyse
d = dir(base_path_of_files_to_analyse);
d = d(~ismember({d.name},{'.','..'})); % remove "." & ".."
files = cellfun(@(x) fullfile(base_path_of_files_to_analyse,x),{d.name}','uni',0);
end
%-------------------------------------------------------------
function operate_on_file(file1)
% do something
end
Matlab Pro
Matlab Pro 2023년 12월 25일
please contact for any issue

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Create and Run MATLAB Tests에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by