Unit testing of a script that requires a file as input

28 views (last 30 days)
I wish to create a number of unit tests for my program using the Matlab unit-test framework. However, my program requires a simple input text file to run. Essentially the input file is read line-by-line and assigned to particular variables. The variables are then used in the main program calculation.
For my unit tests I want to create a range of different input files that are read and input to the main calculation. Could someone please advise how this can be achieved?
Thanks!
  2 Comments
Steven Lord
Steven Lord on 8 Nov 2024 at 15:29
How do you specify the "simple input text file"? If you call input or use another (graphically) interactive method to select the file, it may be difficult to test this.
If you define a variable containing the name of the text file and then run the script, that would be easier.
If you changed your script to a function that accepts as its input the name of the text file, that would probably be easiest.
Martin Doherty
Martin Doherty on 8 Nov 2024 at 15:46
Edited: Martin Doherty on 8 Nov 2024 at 15:50
Hi Steven,
You are correct, I use input to ask for the filename in the command window:
% Ask user for input filename and validate that it exists in current path. If not,
% terminate program.
prompt = 'Enter Input filename: ';
IPFILE = input(prompt,'s');
input_file_flag = exist(IPFILE,'file');
if input_file_flag == 0
disp('ERROR: Entered input filename does not exist. Terminating Program.')
return
end
The input filename contained in IPFILE is then passed to a function which parses input file and assigns variables.
As you mention, I could change my script to a function with IPFILE being the only input. This would be suitable for testing purposes and require trivial changes to my script as it stands. I would have to pass output from the function into the unit test, this might be quite cumbersome. Thanks for the advice,
Martin

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 8 Nov 2024 at 16:46
I would split your script into two pieces:
  • An interface piece that calls input. This piece would not be used during automated testing; you could interactively test it with say two cases, one with a file that exists and one with a file that doesn't. In practice, this script could call the other script.
  • A computational piece that does not call input. It assumes the variable containing the file name exists when it starts running (or could assert early on in its execution that the variable does exist.) This you could unit test by defining the variable, running the script, and checking various quantities and qualiites of the variables created inside the script using one of the unit testing approaches (script-based tests, function-based tests, or class-based tests) available in MATLAB.
Script-based tests are the easiest to write, but they don't provide all the functionality that function- or class-based tests provide. Which approach you use depends on your comfort level with scripts, functions, and classes and what types of test functionality you want to use (parameterized testing, for example.)

Tags

Community Treasure Hunt

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

Start Hunting!