matlab.unittest.Test 类
命名空间: matlab.unittest
超类: matlab.unittest.TestSuite
单一测试的设定
描述
matlab.unittest.Test
类保留测试运行器运行单个测试所需的所有信息。Test
对象,通常称为 Test
元素,是测试套件中包含的基本元素。
例如,当您使用 testsuite
函数创建一个测试套件时,得到的 TestSuite
数组的元素是 Test
元素。您无法直接创建 matlab.unittest.Test
类的对象。
属性
Name
— 测试名称
字符向量
测试的名称,以字符向量形式返回。对于一个给定的测试文件,测试的名称唯一地标识测试内容的最小可运行部分。它包括命名空间名称、文件名(不包括扩展名)、过程名称和关于参数化的信息。
属性:
GetAccess | public |
SetAccess | immutable |
ProcedureName
— 测试过程的名称
字符向量
必须运行的测试过程的名称,以字符向量形式返回。在基于类的测试中,测试过程的名称是包含该测试的 Test
方法的名称。在基于函数的测试中,它是包含测试的局部函数的名称。在基于脚本的测试中,它是从测试部分标题生成的名称。与测试的名称不同,测试过程的名称不包括任何命名空间名称、文件名或关于参数化的信息。
属性:
GetAccess | public |
SetAccess | immutable |
TestClass
— 测试类的名称
字符串标量
定义测试的测试类的名称,以字符串标量形式返回。如果该测试不是基于类的测试,则属性包含空字符串数组。
属性:
GetAccess | public |
SetAccess | private |
BaseFolder
— 包含测试文件的文件夹的完整路径
字符向量
包含测试文件的文件夹的完整路径,以字符向量形式返回。对于命名空间中定义的测试,基本文件夹是顶级包文件夹的父级。
属性:
GetAccess | public |
SetAccess | 限制访问 |
Parameterization
— 运行测试所需的参数化数据
matlab.unittest.parameters.Parameter
对象数组
运行测试所需的参数化数据,以 matlab.unittest.parameters.Parameter
对象数组形式返回。此属性包含与测试相关的信息,如参数化属性名称、参数名称和值。
属性:
GetAccess | public |
SetAccess | private |
SharedTestFixtures
— 共享测试脚手架
matlab.unittest.fixtures.Fixture
对象数组
测试所需的共享测试脚手架,以 matlab.unittest.fixtures.Fixture
对象数组形式返回。此属性包含 TestCase
类的 SharedTestFixtures
类级别特性指定的所有脚手架。
属性:
GetAccess | public |
SetAccess | private |
Tags
— 测试的标记
字符向量元胞数组
测试的标记,以字符向量元胞数组形式返回。测试标记是用 TestCase
类的 TestTags
类级别属性或 TestTags
方法级别属性指定的。
属性:
GetAccess | public |
SetAccess | private |
示例
创建测试元素数组
从测试类创建一个由 Test
元素组成的数组。
在当前文件夹中名为 ZerosTest.m
的文件中,创建 ZerosTest
类来测试 zeros
函数。
classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end methods (Test) function testClass(testCase,size,type) testCase.verifyClass(zeros(size,type),type) end function testSize(testCase,size) testCase.verifySize(zeros(size),size) end function testDefaultClass(testCase) testCase.verifyClass(zeros,"double") end function testDefaultSize(testCase) testCase.verifySize(zeros,[1 1]) end function testDefaultValue(testCase) testCase.verifyEqual(zeros,0) end end end
根据 ZerosTest
类创建一个测试套件。返回的 TestSuite
数组的每个元素均为一个 matlab.unittest.Test
对象。
suite = testsuite("ZerosTest"); whos suite
Name Size Bytes Class Attributes suite 1x11 27442 matlab.unittest.Test
显示 Test
元素的名称。每个名称包括类名称和对应的 Test
方法名称。对于参数化测试,元素名称还包含关于参数化的信息。
disp({suite.Name}')
{'ZerosTest/testClass(size=s2d,type=single)'} {'ZerosTest/testClass(size=s2d,type=double)'} {'ZerosTest/testClass(size=s2d,type=uint16)'} {'ZerosTest/testClass(size=s3d,type=single)'} {'ZerosTest/testClass(size=s3d,type=double)'} {'ZerosTest/testClass(size=s3d,type=uint16)'} {'ZerosTest/testSize(size=s2d)' } {'ZerosTest/testSize(size=s3d)' } {'ZerosTest/testDefaultClass' } {'ZerosTest/testDefaultSize' } {'ZerosTest/testDefaultValue' }
版本历史记录
在 R2013a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)