Main Content

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

matlab.unittest.plugins.QualifyingPlugin 类

命名空间: matlab.unittest.plugins
超类: matlab.unittest.plugins.TestRunnerPlugin

执行系统范围内验证的插件的接口

描述

使用有别于测试内容的限定插件来生成测试失败。插件级别的验证会很有用,因为您可以避免在每个测试中重复相同的验证。您只需将该插件添加到特定测试会话的测试运行器,即可决定对测试套件定期应用系统范围的验证。

QualifyingPlugin 接口使测试运行器插件作者能够实现对测试套件执行系统范围验证的插件。在以下继承的方法中,可以执行验证、假设、断言和致命断言:

  • setupTestClass

  • teardownTestClass

  • setupTestMethod

  • teardownTestMethod

在以下继承的方法中,只能执行假设、断言和致命断言:

  • setupSharedTestFixture

  • teardownSharedTestFixture

方法

assertUsing断言值满足给定约束
assumeUsing假设值满足给定约束
fatalAssertUsing致命断言值满足给定约束
verifyUsing确认值满足给定约束

继承的方法

createSharedTestFixture 扩展共享测试脚手架的创建
createTestClassInstance 扩展类级别 TestCase 实例的创建
createTestMethodInstance 扩展方法级别 TestCase 实例的创建
reportFinalizedResult为最终化测试结果扩展报告
reportFinalizedSuite扩展为最终化的 TestSuite 数组生成的报告
runSession扩展测试会话的运行
runTest 扩展单个 Test 元素的运行
runTestClass 从同一个类或函数扩展 Test 元素的运行
runTestMethod 扩展单个 Test 方法的运行
runTestSuite扩展 TestSuite 数组的运行
setupSharedTestFixture扩展共享测试脚手架的设置
setupTestClass 扩展测试类的设置
setupTestMethod扩展设置 Test 方法
teardownSharedTestFixture扩展共享测试脚手架的拆解
teardownTestClass扩展拆解测试类
teardownTestMethod扩展拆解 Test 方法

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

创建插件以确保测试文件未更改 MATLAB® 路径。如果运行测试文件之后的路径不同于起始路径,则测试无法正常工作。

创建类 VerifyNoPathChangePlugin,该类继承自 matlab.unittest.plugins.QualifyingPlugin 类。

classdef VerifyNoPathChangePlugin < matlab.unittest.plugins.QualifyingPlugin
    properties (Access=private)
        OriginalPath
    end
    
    methods (Access=protected)
        function setupTestClass(plugin, pluginData)
            plugin.OriginalPath = path;
            setupTestClass@matlab.unittest.plugins.QualifyingPlugin(plugin,pluginData);
        end
        function teardownTestClass(plugin, pluginData)
            import matlab.unittest.constraints.IsEqualTo;
            teardownTestClass@matlab.unittest.plugins.QualifyingPlugin(plugin,pluginData);
            plugin.verifyUsing(pluginData.QualificationContext, ...
                path, IsEqualTo(plugin.OriginalPath), ...
                sprintf('%s modified the path.', pluginData.Name));
        end
    end
end

创建以下测试类。测试会修改路径,但不会恢复原始路径。

classdef LeavesModifiedPath < matlab.unittest.TestCase
    methods (Test)
        function test1(~)
            addpath(pwd);
        end
    end
end

对于此示例,在命令提示符处,从路径中删除存在的工作文件夹。

rmpath(pwd)

创建测试套件,将该插件添加到测试运行器,并运行套件。测试失败,因为该测试后的路径不同于起始路径。

suite = matlab.unittest.TestSuite.fromClass(?LeavesModifiedPath);
runner = matlab.unittest.TestRunner.withTextOutput;
runner.addPlugin(VerifyNoPathChangePlugin);
runner.run(suite);
Running LeavesModifiedPath
.
================================================================================
Verification failed while setting up or tearing down LeavesModifiedPath.
As a result, all LeavesModifiedPath tests failed.

    ----------------
    Test Diagnostic:
    ----------------
    LeavesModifiedPath modified the path.

    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StringComparator failed.
        --> The character arrays are not equal.
    
    Actual char:
        C:\work;C:\Program Files\MATLAB\R2015b\toolbox\matlab\...
   Expected char:
        C:\Program Files\MATLAB\R2015b\toolbox\matlab\...

    ------------------
    Stack Information:
    ------------------
    In C:\work\VerifyNoPathChangePlugin.m (VerifyNoPathChangePlugin.teardownTestClass) at 14
================================================================================

Done LeavesModifiedPath
__________

Failure Summary:

     Name                      Failed  Incomplete  Reason(s)
    =======================================================================
     LeavesModifiedPath/test1    X                 Failed by verification.

版本历史记录

在 R2015b 中推出