Pass data directory input argument to a set of unit tests

5 次查看(过去 30 天)
Have created a class-based unit test and test runner function. Want to execute on several datasets with each dataset located in separate directories. Tried to pass an input argument but cannot figure out how to pass to RUN function and in the classdef function. Any suggestions? Below are my program.
function results = testRunner( dataFoldername )
% Number of Arguments Checks
% Input Arguments
narginchk( 1, 1 );
% Output Arguments
nargoutchk( 1, 1 );
% Data Input Pathname
currentFolder = pwd;
dataPathname = sprintf( '%s%s%s', currentFolder, filesep, dataFoldername );
% Run Unit Tests
testCase = testGantryMotionRTS;
results = run( testCase );
classdef testGantryMotionRTS < matlab.unittest.TestCase
% Test Method Block
methods ( Test )
% Test Function 1
function testBalanceSensorResponseSolution1( testCase )
% Expected Results
expectedResults = waveformcsvread;
% Actual Results
actualResults = rtscsvread( 'balancesensorresponsetopic' );
% Verify Using Test Qualification
for i = 1:length( expectedResults )
actualSolution = actualResults.rotation_time;
expectedSolution = expectedResults.rotation_time;
testCase.verifyEqual( actualSolution, expectedSolution );
actualSolution = actualResults.sensor_data.x_data;
expected
actualSolution = actualResults.sensor_data.z_data;
expectedSolution = expectedResults.sensor_data.z_data;
testCase
actualSolution = actualResults.sensor_data.gantry_position_in_degrees;
expectedSolution = expectedResults.sensor_data.gantry_position_in_degrees;
testCase.verifyEqual( actualSolution, expectedSolution );
end
end
end
end
Thank you
Ariel Friedlander

回答(2 个)

Steven Lord
Steven Lord 2019-1-8
编辑:Steven Lord 2019-1-8
One way to do this would be with a parameterized test, either by having your test call a function that returns the parameter values (the list of data sets) or (if you're using release R2018b or later) using an external parameter that you inject when you execute the test. An example of the former:
classdef testExist < matlab.unittest.TestCase
properties(TestParameter)
listOfFilesInPWD = getAllFilesInPwd;
end
methods(Test)
function existShouldFindFilesInCurrentDirectory(testcase, ...
listOfFilesInPWD)
testcase.verifyNotEqual(exist(listOfFilesInPWD), 0)
end
end
end
function y = getAllFilesInPwd
d = dir;
y = {d.name};
end

Luna
Luna 2019-1-8
Hi Ariel,
You can't define a class and a function which is not a method of the class in the same .m file.
Please read below link to create a user based unit test classes and built-in packages:

类别

Help CenterFile Exchange 中查找有关 Write Unit Tests 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by