Main Content

verifyClass

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

Verify class of specified value

Description

example

verifyClass(testCase,actual,class) verifies that the class of actual is the specified class.

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

example

verifyClass(testCase,actual,class,diagnostic) also associates the diagnostic information in diagnostic with the qualification.

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 a matlab.unittest.TestCase object.

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

Expected class, specified as a string scalar, character vector, or matlab.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

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

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")
Verification passed.

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

verifyClass(testCase,5,?double)
Verification passed.

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)
Verification passed.

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.

actual = ExampleHandle;

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)
Verification passed.

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.

actual = add5(1);

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)
Verification passed.

Tips

  • verifyClass is a convenience method. For example, verifyClass(testCase,actual,class) is functionally equivalent to the following code.

    import matlab.unittest.constraints.IsOfClass
    testCase.verifyThat(actual,IsOfClass(class))
    
  • Use verification qualifications to produce and record failures without throwing an exception. Since verifications do not throw exceptions, all test content runs to completion even when verification failures occur. Typically, verifications are the primary qualification for a unit test, since they typically do not require an early exit from the test. Use other qualification types to test for violation of preconditions or incorrect test setup:

    • Use assumption qualifications to ensure that the test environment meets preconditions that otherwise do not result in a test failure. Assumption failures result in filtered tests, and the testing framework marks the tests as Incomplete. For more information, see matlab.unittest.qualifications.Assumable.

    • Use assertion qualifications when the failure condition invalidates the remainder of the current test content, but does not prevent proper execution of subsequent tests. A failure at the assertion point renders the current test as Failed and Incomplete. For more information, see matlab.unittest.qualifications.Assertable.

    • Use fatal assertion qualifications to abort the test session upon failure. These qualifications are useful when the failure is so fundamental that continuing testing does not make sense. Fatal assertion qualifications are also useful when fixture teardown does not restore the environment state correctly, and aborting testing and starting a fresh session is preferable. For more information, see matlab.unittest.qualifications.FatalAssertable.

Version History

Introduced in R2013a