Main Content

run

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

运行与测试用例对应的测试

说明

示例

result = run(testCase) 运行定义 testCase 的类的所有 Test 方法,并以 matlab.unittest.TestResult 数组形式返回测试运行的结果,其中每个 TestResult 对象对应于一个 Test 方法。

run 方法是用来与 TestCase 子类进行交互式试验的便利方法。它通过使用为文本输出配置的 TestRunner 实例来运行测试。文本输出包括测试运行进度以及在测试失败时的诊断。

示例

result = run(testCase,method) 运行 testCase 的指定 Test 方法。

输入参数

全部展开

测试用例,指定为 matlab.unittest.TestCase 对象。

要运行的 testCaseTest 方法的标识符,指定为字符串标量、字符向量或 matlab.metadata.Method 实例。

属性

Sealedtrue

要了解方法的属性,请参阅方法属性

示例

全部展开

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

在当前文件夹的名为 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

为了以交互方式运行测试,请基于测试类创建一个测试用例。您还可以使用 forInteractiveUse 静态方法来创建一个测试用例。

testCase = FigurePropertiesTest;

运行与测试用例对应的测试。两个测试都通过。

result1 = run(testCase)
Running FigurePropertiesTest
.
.
Done FigurePropertiesTest
__________
result1 = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

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

现在,运行 defaultCurrentPoint Test 方法。

result2 = run(testCase,"defaultCurrentPoint")
Running FigurePropertiesTest
.
Done FigurePropertiesTest
__________
result2 = 
  TestResult with properties:

          Name: 'FigurePropertiesTest/defaultCurrentPoint'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0553
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.055328 seconds testing time.

版本历史记录

在 R2013a 中推出