Is it possible to combine parameterized and non-parameterized tests in the same test class?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Bradley Treeby
el 23 de Ag. de 2023
Comentada: Bradley Treeby
el 23 de Ag. de 2023
I am writing class-based units tests based on matlab.unittest.TestCase. The tests are currently parameterized in the following way, and everything works as expected:
classdef TestSomething < matlab.unittest.TestCase
properties
testParam
end
properties (MethodSetupParameter)
testParamInputs = {[1, 2], [3, 4]}
end
methods (TestMethodSetup, ParameterCombination="sequential")
function setupFunction(testCase, testParamInputs)
testCase.testParam = doSomething(testParamInputs);
end
end
methods (Test, ParameterCombination="sequential")
function testFunction(testCase)
% Test something.
end
end
end
My question is whether it is possible to also include non-parameterized tests (i.e., tests that run only once) in the same class?
I know it's possible if I just define properties(TestParameter) and have some tests that don't take the test parameter/s as an input. But I'm wondering if it's possible in my case given TestMethodSetup is parameterized.
0 comentarios
Respuesta aceptada
Steven Lord
el 23 de Ag. de 2023
My question is whether it is possible to also include non-parameterized tests (i.e., tests that run only once) in the same class?
Absolutely! In fact the example TestCarpet on this documentation page demonstrates this technique. Both the testRemainPixels and testClass test methods will run repeatedly, while the testDefaultL1Output class (which doesn't use any of the TestParameter properties in its signature) will run just once. You can see this in the "Run All Tests" section where we display the Name (including parameterization) of each test method that will run when we run that file. testRemainPixels runs three times, testClass runs nine times, and testDefaultL1Output runs once.
But if you want to see how many times your test will run:
suite = matlab.unittest.TestSuite.fromFile('TestSomething.m');
{suite.Name}.'
In this case I think you probably want to make your MethodSetupParameter into a TestParameter instead, since all your setupFunction is doing is setting a property of the object.
suite = matlab.unittest.TestSuite.fromFile('TestSomething2.m');
{suite.Name}.'
But if you do need to parameterize your TestMethodSetup then the test method will run once per value of the MethodSetupParameter.
Más respuestas (0)
Ver también
Categorías
Más información sobre Write Unit Tests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!