matlab.unittest.TestCase 类
命名空间: matlab.unittest
超类: matlab.unittest.qualifications.Assertable, matlab.unittest.qualifications.Assumable, matlab.unittest.qualifications.FatalAssertable, matlab.unittest.qualifications.Verifiable
所有测试类的超类
描述
matlab.unittest.TestCase 类是 MATLAB® 中所有测试类的超类。它提供了编写并标识测试内容的接口,包括测试脚手架的设置与拆解例程。
创建基于类的测试需要子类化 TestCase 类。为了指定测试和测试脚手架,子类可以利用框架特定的属性。有关详细信息,请参阅 TestCase 类属性、TestCase 方法属性和 TestCase 属性特性。
matlab.unittest.TestCase 类是一个 handle 类。
创建对象
在大多数情况下,您不需要直接创建 TestCase 类的实例。测试运行器在运行测试时会自动创建 TestCase 实例。
要为交互式命令行测试创建 TestCase 实例,请使用 forInteractiveUse 静态方法。
方法
事件
除了这些事件之外,matlab.unittest.TestCase 类还从 Assertable、Assumable、FatalAssertable 和 Verifiable 类继承事件。
| 事件名称 | 触发器 | 事件数据 | 事件属性 |
|---|---|---|---|
ExceptionThrown | 当测试运行器在测试内容中捕获到异常时触发。系统会将一个 ExceptionEventData 对象传递给侦听程序回调函数。 | matlab.unittest.qualifications.ExceptionEventData |
|
DiagnosticLogged | 在调用 log 方法时触发。LoggedDiagnosticEventData 对象传递给侦听程序回调函数。 | matlab.unittest.diagnostics.LoggedDiagnosticEventData |
|
示例
详细信息
提示
不推荐在
TestCase子类中定义构造函数或析构函数方法。TestCase构造函数和析构函数方法不被视为测试内容,不应用于执行鉴定。例如,SampleTest类使用构造函数方法和Test方法指定鉴定。然而,构造函数方法中的鉴定不会产生测试失败。测试框架仅报告一次测试失败,它是在testSize方法中执行鉴定的结果。classdef SampleTest < matlab.unittest.TestCase methods function testCase = SampleTest % Constructor method not recommended testCase.verifyEqual(1,2) % Does not produce a test failure end end methods (Test) function testSize(testCase) testCase.verifySize([1 2 3; 4 5 6],[2 4]) % Produces a test failure end end end