Main Content

verifyInstanceOf

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

Verify value is instance of specified class

Description

verifyInstanceOf(testCase,actual,class) verifies that actual is an instance of the specified class.

This method verifies inclusion in a class hierarchy. For the test to pass, the specified class must be either the class or a superclass of actual. To verify an exact class match, use verifyClass.

example

verifyInstanceOf(testCase,actual,class,diagnostic) 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 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 a numeric value is an instance of the class double.

verifyInstanceOf(testCase,1,"double")
Verification passed.

Test if the value is an instance of the class logical. The test fails.

verifyInstanceOf(testCase,1,"logical", ...
    "Value must be an instance of the class logical.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Value must be an instance of the class logical.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyInstanceOf failed.
    --> The value must be an instance of the expected type.
        
        Actual Class:
            double
        Expected Type:
            logical
    
    Actual Value:
         1
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestANumericValueExample.m (TestANumericValueExample) at 17

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that @sin is a function handle.

verifyInstanceOf(testCase,@sin,"function_handle")
Verification passed.

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

verifyInstanceOf(testCase,@sin,?function_handle)
Verification passed.

Use verifyInstanceOf to test inclusion in a class hierarchy.

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 actual is an instance of the ExampleHandle class.

testCase = matlab.unittest.TestCase.forInteractiveUse;
verifyInstanceOf(testCase,actual,?ExampleHandle)
Verification passed.

Test if actual is an instance of the handle class. The test passes because ExampleHandle derives from the handle class.

verifyInstanceOf(testCase,actual,?handle)
Verification passed.

Tips

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

    import matlab.unittest.constraints.IsInstanceOf
    testCase.verifyThat(actual,IsInstanceOf(class))
    
  • Use verification qualifications to produce and record failures without throwing an exception. Because 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 because 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