matlab.unittest.TestSuite.sortByFixtures - Reorder test suite based on shared fixtures - MATLAB (original) (raw)

Main Content

Class: matlab.unittest.TestSuite
Namespace: matlab.unittest

Reorder test suite based on shared fixtures

Syntax

sortedSuite = sortByFixtures(suite) [sortedSuite,I] = sortByFixtures(suite)

Description

[sortedSuite](#mw%5F1b756324-d638-4c7a-9872-50938d15ea6b) = sortByFixtures([suite](#mw%5Fd80d5487-7643-4233-b528-1f419d68dfe7)) reorders the test suite to reduce shared fixture setup and teardown operations. Do not rely on the order of elements in sortedSuite as it might change in a future release.

[[sortedSuite](#mw%5F1b756324-d638-4c7a-9872-50938d15ea6b),[I](#d126e1845660)] = sortByFixtures([suite](#mw%5Fd80d5487-7643-4233-b528-1f419d68dfe7)) also returns a sort index I that describes the arrangement of the elements of suite into sortedSuite.

Input Arguments

expand all

Set of tests, specified as a matlab.unittest.Test array.

Output Arguments

expand all

Ordered set of tests, returned as a matlab.unittest.Test array.sortedSuite is a permutation of the test elements ofsuite.

Sort index, returned as a vector, matrix, or multidimensional array.I is the same size as suite and describes the arrangement of the elements of suite intoorderedSuite. Specifically, sortedSuite = suite(I).

Examples

expand all

Create three test classes in your current working folder. Test classes MyTestClassA and MyTestClassC use the same shared path fixture.

classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture('offPathFolder')}) ... MyTestClassA < matlab.unittest.TestCase methods (Test) function test_A(testCase) % test content end end end

classdef MyTestClassB < matlab.unittest.TestCase methods (Test) function test_B(testCase) % test content end end end

classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture('offPathFolder')}) ... MyTestClassC < matlab.unittest.TestCase methods (Test) function test_C(testCase) % test content end end end

Create a test suite from each class.

import matlab.unittest.TestSuite; suiteA = TestSuite.fromClass(?MyTestClassA); suiteB = TestSuite.fromClass(?MyTestClassB); suiteC = TestSuite.fromClass(?MyTestClassC);

Concatenate the suites and view the order of test elements.

suite = [suiteA suiteB suiteC]; {suite.Name}'

ans =

3×1 cell array

{'MyTestClassA/test_A'}
{'MyTestClassB/test_B'}
{'MyTestClassC/test_C'}

Sort the suite by shared fixtures and view the order of test elements.

sortedSuite = sortByFixtures(suite); {sortedSuite.Name}'

ans =

3×1 cell array

{'MyTestClassA/test_A'}
{'MyTestClassC/test_C'}
{'MyTestClassB/test_B'}

Since the tests in MyTestClassA andMyTestClassC have the same shared test fixture, the test elements are reordered so that they are adjacent in the suite.

Tips

If you create a test suite using a single call to the testsuite function instead of several calls to a method of matlab.unittest.TestSuite, the suite is automatically sorted based on shared fixtures. However, if you add, remove, or reorder elements after initial suite creation, call the sortByFixtures method to sort the suite.

Version History

Introduced in R2018b