Filtering test cases out of a test suite
52 views (last 30 days)
Show older comments
Liam
on 9 Dec 2024 at 15:34
Commented: Liam
on 9 Dec 2024 at 17:00
Hello,
I am writing a script that takes in a matlab.unittest.TestSuite object that contains several test cases from tests (call this testSuiteBeforeFiltering). I am then building a second TestSuite object of test cases that I would like to filter out of the original testSuite (call this invalidTests).
What would be desirable here is if there was an easy way to compare the two TestSuite objects, and get a new TestSuite of only the tests between them that are different. I came up with one way to do this, but it seems to break if the TestSuite that is passed in has had any paramters overwritten. This is close to what I think would work:
% Valid tests are tests within testSuiteBeforeFiltering, but not within invalidTests
% testSuiteBeforeFiltering and invalidTests are both TestSuites
suiteAllTests = {testSuiteBeforeFiltering.Name}';
suiteFilteredTests = {invalidTests.Name}';
% Find the test names that are different, then create the test suite
goodTests = setdiff(suiteAllTests, suiteFilteredTests);
testSuite = testsuite(goodTests);
However, the testsuite() function errors out if any parameters have been given a new value. This error message gets generated:
"
Incorrect number or types of inputs or outputs for function createSuiteFromName.
Error using matlab.unittest.internal.createTestSuite (line 21)
"ExampleDirectory.ExampleDirectory2.ExampleDirectory3.UnitTestName[ClassParameter=ClassParameter#ext]/TestMethodName(TestParameter1=1,TestParameter2=1)"
is not recognized as a valid test entity.
"
It seems like the part it does not like is the "ClassParameter=ClassParameter#ext". I was wondering if there is a way to create a TestSuite that contains only the elements that are different between two other TestSuites. Thanks!
0 Comments
Accepted Answer
Jacob Mathew
on 9 Dec 2024 at 16:43
Hi Liam,
You can create two testsuites and then use the setdiff function to identify the indices of the testpoints that you and indexing them alone to create the filtered testsuite. Here is an example:
% addNumbers.m is the function we are testing -|
% TestAddNumbers.m contains all the tests -|->Attached with answer
% unwanted.m contains the tests that we don't need -|
% Creating the test suite for TestAddNumbers & unwanted
suiteAddNumbers = matlab.unittest.TestSuite.fromFile('TestAddNumbers.m');
suiteUnwanted = matlab.unittest.TestSuite.fromFile('unwanted.m');
% Extract the names of the test cases in each suite
% The names are extracted as 'testFileName/testPoint'
addNumbersTestNames = extractAfter({suiteAddNumbers.Name}, '/')
unwantedTestNames = extractAfter({suiteUnwanted.Name}, '/')
% Find the indices of tests in suiteAddNumbers that are not in suiteUnwanted
[~, uniqueIndices] = setdiff(addNumbersTestNames, unwantedTestNames, 'stable')
% Create the modified test suite using the unique indices
modifiedSuite = suiteAddNumbers(uniqueIndices);
filteredTestNames = extractAfter({modifiedSuite.Name}, '/')
You can refer to the documentation of setdiff in the following link:
More Answers (1)
Steven Lord
on 9 Dec 2024 at 15:58
I am writing a script that takes in a matlab.unittest.TestSuite object that contains several test cases from tests (call this testSuiteBeforeFiltering). I am then building a second TestSuite object of test cases that I would like to filter out of the original testSuite (call this invalidTests).
What are you trying to do when performing this filtering? Depending on what exactly you're trying to do the selectIf method for matlab.unittest.TestSuite objects may be of use. For example, if the test cases you want to filter out all have a specific test tag that none of the tests you want to keep have, you could use the negation of matlab.unittest.selectors.HasTag to filter out just those test cases. See the third example on the selectIf documentation page for an example of this usage.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!