matlab.unittest.qualifications.Verifiable.verifyClass - Verify class of specified value - MATLAB (original) (raw)

Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications

Verify class of specified value

Syntax

Description

verifyClass([testCase](#bt00p0m-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F7e5a60d4-1349-4066-8d73-592268712140),[class](#mw%5F403e84f4-4236-487a-b241-c03a315574ed)) verifies that the class of actual is the specified class.

This method verifies an exact class match. To verify inclusion in a class hierarchy, useverifyInstanceOf.

example

verifyClass([testCase](#bt00p0m-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F7e5a60d4-1349-4066-8d73-592268712140),[class](#mw%5F403e84f4-4236-487a-b241-c03a315574ed),[diagnostic](#mw%5F222aa152-f33e-4484-ae57-7e3a6b091c44)) also associates the diagnostic information in diagnostic with the qualification.

example

Input Arguments

expand all

Test case, specified as a matlab.unittest.qualifications.Verifiable object. Because the matlab.unittest.TestCase class subclasses matlab.unittest.qualifications.Verifiable and inherits its methods, testCase is typically amatlab.unittest.TestCase object.

Value to test, specified as a value of any data type.

Expected class, specified as a string scalar, character vector, ormatlab.metadata.Class instance.

Example: "MyClass"

Example: ?MyClass

Diagnostic information to display when the qualification passes or fails, specified as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

Example: "My Custom Diagnostic"

Example: @dir

Examples

expand all

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that the class of the numeric value 5 is double.

verifyClass(testCase,5,"double")

Repeat the test using a matlab.metadata.Class instance instead of a string.

verifyClass(testCase,5,?double)

Test if zero is a logical value. The test fails.

verifyClass(testCase,0,"logical","Value must be logical.")

Verification failed. ---------------- Test Diagnostic: ---------------- Value must be logical. --------------------- Framework Diagnostic: --------------------- verifyClass failed. --> The value's class is incorrect.

    Actual Class:
        double
    Expected Class:
        logical

Actual Value:
     0
------------------
Stack Information:
------------------
In C:\work\TestNumericValuesExample.m (TestNumericValuesExample) at 21

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that @sin is a function handle.

verifyClass(testCase,@sin,?function_handle)

Repeat the test using the function name "sin". The test fails.

verifyClass(testCase,"sin",?function_handle)

Verification failed. --------------------- Framework Diagnostic: --------------------- verifyClass failed. --> The value's class is incorrect.

    Actual Class:
        string
    Expected Class:
        function_handle

Actual Value:
    "sin"
------------------
Stack Information:
------------------
In C:\work\TestAFunctionHandleExample.m (TestAFunctionHandleExample) at 17

Use the verifyClass method to test for exact class match.

In a file in your current folder, create the ExampleHandle handle class.

classdef ExampleHandle < handle properties Number = 1; end end

Create an instance of the defined class.

Create a test case for interactive testing, and then verify that the class of actual is ExampleHandle.

testCase = matlab.unittest.TestCase.forInteractiveUse; verifyClass(testCase,actual,?ExampleHandle)

Repeat the test using the handle class. The test fails because handle is not the exact class of the actual value.

verifyClass(testCase,actual,?handle)

Verification failed. --------------------- Framework Diagnostic: --------------------- verifyClass failed. --> The value's class is incorrect.

    Actual Class:
        ExampleHandle
    Expected Class:
        handle

Actual Value:
  ExampleHandle with properties:

    Number: 1
------------------
Stack Information:
------------------
In C:\work\TestInstanceOfADerivedClassExample.m (TestInstanceOfADerivedClassExample) at 26

Use verifyClass to test the class of the output of a function.

In a file in your current folder, create the add5 function. The function accepts a numeric input and increments it by five.

function y = add5(x) % add5 - Increment input by 5 if ~isa(x,"numeric") error("add5:InputMustBeNumeric","Input must be numeric.") end y = x + 5; end

Call the function with a valid input.

Create a test case for interactive testing, and then verify that the class of actual is double.

testCase = matlab.unittest.TestCase.forInteractiveUse; verifyClass(testCase,actual,?double)

Tips

Version History

Introduced in R2013a