Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

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 类。

类属性

Abstract
true

有关类属性的信息,请参阅类属性

创建对象

在大多数情况下,您不需要直接创建 TestCase 类的实例。测试运行器在运行测试时会自动创建 TestCase 实例。

要为交互式命令行测试创建 TestCase 实例,请使用 forInteractiveUse 静态方法。

方法

全部展开

事件

下表列出了 TestCase 类的事件。除了这些事件之外,TestCase 类还从 AssertableAssumableFatalAssertableVerifiable 类继承事件。

事件名称触发器事件数据事件属性
ExceptionThrown当测试运行器在测试内容中捕获到异常时触发。系统会将一个 ExceptionEventData 对象传递给侦听程序回调函数。matlab.unittest.qualifications.ExceptionEventData

NotifyAccess: private

ListenAccess: public

DiagnosticLogged在调用 log 方法时触发。LoggedDiagnosticEventData 对象传递给侦听程序回调函数。matlab.unittest.diagnostics.LoggedDiagnosticEventData

NotifyAccess: private

ListenAccess: public

示例

全部折叠

通过创建和运行测试类来测试图窗的属性。

在当前文件夹的名为 FigurePropertiesTest.m 的文件中,通过以下操作创建 FigurePropertiesTest 测试类:

  • 子类化 matlab.unittest.TestCase

  • 添加属性来表示要测试的图窗

  • TestMethodSetup methods 代码块中的每个测试添加设置和拆解代码

  • Test methods 代码块中添加测试

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
            testCase.addTeardown(@close,testCase.TestFigure)
        end
    end

    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                "Default current point must be [0 0].")
        end

        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                "Default current object must be empty.")
        end
    end
end

运行测试类中的测试。在此示例中,两个测试都通过。

results = runtests("FigurePropertiesTest")
Running FigurePropertiesTest
.
.
Done FigurePropertiesTest
__________
results = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   1.1899 seconds testing time.

详细信息

全部展开

提示

  • 不推荐在 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
    

版本历史记录

在 R2013a 中推出

全部展开