Unit testing of a script that requires a file as input

27 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2024-11-8,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 2024-11-8,15:46
编辑:Martin Doherty 2024-11-8,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

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2024-11-8,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.)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by