Pass data directory input argument to a set of unit tests
5 views (last 30 days)
Show older comments
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
1 Comment
Answers (2)
Steven Lord
on 8 Jan 2019
Edited: Steven Lord
on 8 Jan 2019
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
0 Comments
Luna
on 8 Jan 2019
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:
0 Comments
See Also
Categories
Find more on Write Unit Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!