Is it possible to combine parameterized and non-parameterized tests in the same test class?

2 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord on 23 Aug 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}.'
ans = 2×1 cell array
{'TestSomething/[testParamInputs=1x2_double]testFunction' } {'TestSomething/[testParamInputs=1x2_double_1]testFunction'}
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}.'
ans = 1×1 cell array
{'TestSomething2/testFunction'}
But if you do need to parameterize your TestMethodSetup then the test method will run once per value of the MethodSetupParameter.
  1 Comment
Bradley Treeby
Bradley Treeby on 23 Aug 2023
Thanks for your insights!
In my case, the setupFunction is setting a property (several in the real case) that depends on the specific value of testParamInputs. This is reused by the parameterized tests (avoids WET). I couldn't immediately see how to do this using TestParameter?
Thinking about it, I guess if the method setup / teardown is parameterized, then by definition all of the tests must be parameterized.
Maybe one option would be to remove the TestMethodSetup altogether and make setupFunction a regular class method, and call it manually at the beginning of each parameterized test (maybe that's what you meant). Feels more hacky though.

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!