Main Content

sortByFixtures

类: matlab.unittest.TestSuite
命名空间: matlab.unittest

根据共享脚手架对测试套件重新排序

语法

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

说明

sortedSuite = sortByFixtures(suite) 对测试套件重新排序以减少共享脚手架的设置和拆解操作。不要依赖 sortedSuite 中元素的顺序,因为它可能会在未来的版本中更改。

[sortedSuite,I] = sortByFixtures(suite) 还会返回排序索引 I,它描述 suite 的元素在 sortedSuite 中的排列。

输入参数

全部展开

测试集,指定为 matlab.unittest.Test 数组。

输出参量

全部展开

有序测试集,以 matlab.unittest.Test 数组形式返回。sortedSuitesuite 的测试元素的一种置换。

排序索引,以向量、矩阵或多维数组形式返回。Isuite 的大小相同,描述 suite 元素在 orderedSuite 中的排列。具体而言,sortedSuite = suite(I)

示例

全部展开

在当前工作文件夹中创建三个测试类。使用同一共享路径脚手架测试类 MyTestClassAMyTestClassC

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

基于每个类创建一个测试套件。

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

串联套件并查看测试元素的顺序。

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

  3×1 cell array

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

按共享脚手架对套件排序并查看测试元素的顺序。

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

  3×1 cell array

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

由于 MyTestClassAMyTestClassC 中的测试具有相同的共享测试脚手架,因此会对测试元素重新排序,以便它们在套件中的位置相邻。

提示

如果通过单次调用 testsuite 函数(而不是多次调用 matlab.unittest.TestSuite 的方法)创建测试套件,则该套件将基于共享脚手架自动排序。但是,如果在创建初始套件后添加、删除元素或对其重新排序,请调用 sortByFixtures 方法对套件进行排序。

版本历史记录

在 R2018b 中推出