标记单元测试
您可以使用测试标记将测试划分为多个类别,然后运行具有指定标记的测试。典型的测试标记可标识特定功能或描述测试类型。
标记测试
要定义测试标记,请使用有意义的字符向量构成的元胞数组,或使用字符串数组。例如,TestTags = {'Unit'} 或 TestTags = ["Unit","FeatureA"]。
要标记单独的测试,请使用
TestTags方法属性。要标记一个类中的所有测试,请使用
TestTags类属性。如果您在超类中使用TestTags类属性,则子类中的测试会继承这些标记。
此示例测试类 ExampleTagTest 使用 TestTags 方法属性来标记单个测试。
classdef ExampleTagTest < matlab.unittest.TestCase methods (Test) function testA (testCase) % test code end end methods (Test, TestTags = {'Unit'}) function testB (testCase) % test code end function testC (testCase) % test code end end methods (Test, TestTags = {'Unit','FeatureA'}) function testD (testCase) % test code end end methods (Test, TestTags = {'System','FeatureA'}) function testE (testCase) % test code end end end
类 ExampleTagTest 中的若干测试会被标记。例如,使用 'Unit' 和 'FeatureA' 标记 testD。未标记其中一个测试 testA。
此示例测试类 ExampleTagClassTest 使用 TestTags 类属性来标记该类中的所有测试,并使用 TestTags 方法属性将标记添加到单个测试。
classdef (TestTags = {'FeatureB'}) ... ExampleTagClassTest < matlab.unittest.TestCase methods (Test) function testF (testCase) % test code end end methods (Test, TestTags = {'FeatureC','System'}) function testG (testCase) % test code end end methods (Test, TestTags = {'System','FeatureA'}) function testH (testCase) % test code end end end
使用 'FeatureB' 对类 ExampleTagClassTest 中的每个测试进行标记。此外,使用多种标记(包括 'FeatureA'、'FeatureC' 和 'System')来标记单独的测试。
选择并运行测试
选择并运行标记的测试有三种方式:
使用 runtests 运行选定的测试
使用 runtests 函数选择并运行测试,而不需要显式创建测试套件。选择并运行 ExampleTagTest 和 ExampleTagClassTest 中所有包含 'FeatureA' 标记的测试。
results = runtests({'ExampleTagTest','ExampleTagClassTest'},'Tag','FeatureA');
Running ExampleTagTest .. Done ExampleTagTest __________ Running ExampleTagClassTest . Done ExampleTagClassTest __________
runtests 选择并运行了三个测试。
在表中显示结果。
table(results)
ans =
3×6 table
Name Passed Failed Incomplete Duration Details
___________________________ ______ ______ __________ __________ ____________
'ExampleTagTest/testE' true false false 0.00039529 [1×1 struct]
'ExampleTagTest/testD' true false false 0.00045658 [1×1 struct]
'ExampleTagClassTest/testH' true false false 0.00043899 [1×1 struct]选定的测试为 ExampleTagTest 中的 testE 和 testD 以及 ExampleTagClassTest 中的 testH。
使用 TestSuite 方法选择测试
为 ExampleTagTest 类中使用 'FeatureA' 标记的测试创建测试套件。
import matlab.unittest.TestSuite sA = TestSuite.fromClass(?ExampleTagTest,'Tag','FeatureA');
为 ExampleTagClassTest 类中使用 'FeatureC' 标记的测试创建测试套件。
sB = TestSuite.fromFile('ExampleTagClassTest.m','Tag','FeatureC');
串联套件并查看测试名称。
suite = [sA sB];
{suite.Name}'ans =
3×1 cell array
'ExampleTagTest/testE'
'ExampleTagTest/testD'
'ExampleTagClassTest/testG'使用 HasTag 选择器选择测试
为 ExampleTagTest 和 ExampleTagClassTest 类中的所有测试创建测试套件。
import matlab.unittest.selectors.HasTag sA = TestSuite.fromClass(?ExampleTagTest); sB = TestSuite.fromFile('ExampleTagClassTest.m'); suite = [sA sB];
选择所有不含标记的测试。
s1 = suite.selectIf(~HasTag)
s1 =
Test with properties:
Name: 'ExampleTagTest/testA'
ProcedureName: 'testA'
TestClass: "ExampleTagTest"
BaseFolder: 'C:\work'
Parameterization: [0×0 matlab.unittest.parameters.EmptyParameter]
SharedTestFixtures: [0×0 matlab.unittest.fixtures.EmptyFixture]
Tags: {1×0 cell}
Tests Include:
0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
选择所有包含 'Unit' 标记的测试并显示其名称。
s2 = suite.selectIf(HasTag('Unit'));
{s2.Name}'ans =
3×1 cell array
'ExampleTagTest/testD'
'ExampleTagTest/testB'
'ExampleTagTest/testC'使用约束选择所有包含 'FeatureB' 或 'System' 标记的测试。
import matlab.unittest.constraints.IsEqualTo constraint = IsEqualTo('FeatureB') | IsEqualTo('System'); s3 = suite.selectIf(HasTag(constraint)); {s3.Name}'
ans =
4×1 cell array
'ExampleTagTest/testE'
'ExampleTagClassTest/testH'
'ExampleTagClassTest/testG'
'ExampleTagClassTest/testF'另请参阅
matlab.unittest.constraints | matlab.unittest.selectors.HasTag | matlab.unittest.TestSuite | runtests | matlab.unittest.TestCase