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

2 次查看(过去 30 天)
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.

采纳的回答

Steven Lord
Steven Lord 2023-8-23
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 个评论
Bradley Treeby
Bradley Treeby 2023-8-23
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write Unit Tests 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by