Data driven Unit Tests
Mostrar comentarios más antiguos
Is there a way to have data driven unit tests using the unit test framework in MATLAB? I have a TestCase class that is set up to test let say a single file.
I would then like to repeat that TestCase with all the methods on several files. I know I can make many copies of the test and set a property in the class that points to a file. But now I have several TestCase files that have exactly the same code with the only difference is a property that points to a different file.
Ideally I would like to have a list of something like files or values for a property and re-run the tests for each item in the list.
Thanks.
Respuesta aceptada
Más respuestas (2)
David Hruska
el 7 de Mzo. de 2014
Another possibility, as of release 2014a, is to write a parameterized test. Here's a basic skeleton showing how such a test might look:
classdef FileTest < matlab.unittest.TestCase
properties(TestParameter)
File = {'List','Of','Files','To','Be','Tested'};
end
methods(Test)
function testOne(testCase, File)
% The framework calls this test method once for each value of
% the "File" property, each time passing in a single value to
% be used in this test.
disp(['The value of File is: ', File]);
end
end
end
And here's the output of running the test:
>> run(FileTest)
Running FileTest
The value of File is: List
.The value of File is: Of
.The value of File is: Files
.The value of File is: To
.The value of File is: Be
.The value of File is: Tested
.
Done FileTest
__________
ans =
1x6 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Totals:
6 Passed, 0 Failed, 0 Incomplete.
0.001732 seconds testing time.
The framework also includes features for selecting various portions of your suite based on the parameterization.
Sean de Wolski
el 12 de Nov. de 2013
Editada: Sean de Wolski
el 12 de Nov. de 2013
I think if I was going to do this, I would have a separate class that contains the list of files and a run() method.
This class' run() would create the TestCase object (your custom class inheriting from matlab.unittest.TestCase ) whose constructor would take the filename, assign this as a property, and then when it was run, would apply the various methods to this file. For example with the two attached files
F = Filer(pwd)
run(F)
F.Results
To see it fail delete a *.txt file from pwd after creation of the Filer object.
Categorías
Más información sobre Hypothesis Tests en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!