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
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.
回答(1 个)
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.)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!