Main Content

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

matlab.unittest.TestCase.forInteractiveUse

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

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

说明

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

示例

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

示例

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass,ApplySharedTestFixtures=tf) 还指定是否为交互式测试应用与测试类相关联的任何共享测试脚手架。如果 tftrue,则当该方法从 testClass 中创建一个交互式测试用例时,它将设置共享测试脚手架。当测试用例超出范围时,测试框架会自动拆解这些脚手架。共享测试脚手架是使用 TestCase 子类的 SharedTestFixtures 属性指定的。 (自 R2024a 起)

示例

输入参数

全部展开

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

示例: ?ExampleTest

自 R2024a 起

应用与 testClass 相关联的共享测试脚手架的选项,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,当从测试类中创建一个交互式测试用例时,该方法会忽略共享测试脚手架。

属性

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.

自 R2024a 起

当您以交互方式运行测试类的 Test 方法时,请使用共享测试脚手架。

此示例假设您的当前文件夹中包含名为 helperFiles 的子文件夹。如果该子文件夹不存在,请创建它。

[~,~] = mkdir("helperFiles")

在您的当前文件夹下的文件中,创建使用两个共享测试脚手架的 SampleTest 检验类。为了说明目的,在此示例中,Test 方法访问这些脚手架来执行其鉴定。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture("helperFiles"), ...
        matlab.unittest.fixtures.TemporaryFolderFixture}) ...
        SampleTest < matlab.unittest.TestCase
    methods (Test)
        function testFixtureCount(testCase)
            % Test the number of shared test fixtures
            f = testCase.getSharedTestFixtures;
            testCase.verifyNumElements(f,2)
        end

        function testPath(testCase)
            % Test the search path
            import matlab.unittest.constraints.ContainsSubstring
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.PathFixture");
            testCase.verifyThat(path,ContainsSubstring(f.Folder))
        end

        function testTempFolder(testCase)
            % Test writing to the temporary folder
            import matlab.unittest.constraints.IsFile
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.TemporaryFolderFixture");
            tempFolderName = f.Folder;
            filename = string(tempFolderName) + filesep + "myFile.dat";
            writematrix(magic(20),filename)
            testCase.verifyThat(filename,IsFile)
        end
    end
end

创建一个供交互测试的 SampleTest 类的实例。由于 SampleTest 中的测试依赖共享测试脚手架,请在创建测试用例时应用脚手架。

testCase = matlab.unittest.TestCase.forInteractiveUse(?SampleTest, ...
    ApplySharedTestFixtures=true);

以交互方式使用该测试用例调用 testFixtureCount 方法。测试通过,因为共享测试脚手架可供测试使用。如果您创建测试用例而没有应用脚手架,测试将会失败。

testCase.testFixtureCount
Verification passed.

版本历史记录

在 R2014a 中推出

全部展开