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.
댓글 수: 0
채택된 답변
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}.'
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Write Unit Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!