Main Content

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

matlab.unittest.TestCase.forInteractiveUse

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

创建供交互测试的测试用例

说明

示例

testCase = matlab.unittest.TestCase.forInteractiveUse 创建一个为交互式测试配置的测试用例。返回的 TestCase 实例适合在命令提示符下进行试验。它通过向屏幕输出通过和失败事件的消息来响应验证。

示例

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass) 为交互式测试创建指定测试类的实例。

输入参数

全部展开

matlab.unittest.TestCase 派生的测试类,指定为 meta.class 实例。

示例: ?ExampleTest

属性

Statictrue

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

示例

全部展开

测试 actual 值是否包含指定的子字符串。

创建一个供交互测试的测试用例。

testCase = matlab.unittest.TestCase.forInteractiveUse;

定义 actual 值。

actual = "This is a long message.";

验证 actual 包含文本 "long"

verifySubstring(testCase,actual,"long")
Verification passed.

展示大小写不同所造成的影响。此测试失败,因为 actual 不包含 "Long"

verifySubstring(testCase,actual,"Long","Test is case sensitive.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Test is case sensitive.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "Long"
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 22

显示如果子字符串比实际字符串长,测试将失败。

verifySubstring(testCase,actual,"This is a long message with extra words.")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "This is a long message with extra words."
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 27

以交互方式运行测试类的 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 类的实例。

testCase = matlab.unittest.TestCase.forInteractiveUse(?ZerosTest);

以交互方式使用该测试用例调用 testSize 方法。测试通过。

testCase.testSize([5 10])
Verification passed.

版本历史记录

在 R2014a 中推出