verifyClass

Class: matlab.unittest.qualifications.Verifiable
Package: matlab.unittest.qualifications

Verify exact class of specified value

Syntax

verifyClass(verifiable,actual,className)
verifyClass(verifiable,actual,metaClass)
verifyClass(___,diagnostic)

Description

verifyClass(verifiable,actual,className) verifies that actual is a MATLAB® value whose class is the class specified by className.

verifyClass(verifiable,actual,metaClass) verifies that actual is a MATLAB value whose class is the class specified by the meta.class instance metaClass. The instance must be an exact class match. Use verifyInstanceOf to verify inclusion in a class hierarchy.

verifyClass(___,diagnostic) also displays the diagnostic information in diagnostic upon a failure.

Input Arguments

verifiable

The matlab.unittest.TestCase instance which is used to pass or fail the verification in conjunction with the test running framework.

actual

The value to test.

className

Name of class, specified as a character vector.

metaClass

An instance of meta.class.

diagnostic

Diagnostic information related to the qualification, specified as one of the following:

  • string array

  • character array

  • function handle

  • matlab.unittest.diagnostics.Diagnostic object

Diagnostic values can be nonscalar. For more information, see matlab.unittest.diagnostics.Diagnostic.

Examples

expand all

These interactive tests verify the class of the number, 5.

Create a TestCase object and the value to test.

testCase = matlab.unittest.TestCase.forInteractiveUse;
actvalue = 5;

Verify class of actvalue is double.

verifyClass(testCase, actvalue, 'double');
Interactive verification passed.

Verify class of actvalue is char.

verifyClass(testCase, actvalue, 'char');
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
verifyClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        double
    Expected Class:
        char

Actual double:
         5

Test fails.

These interactive tests verify function handles, specified as a meta.class instance, ?function_handle.

Create a TestCase object.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Create a function handle.

fh = @sin;
verifyClass(testCase, fh, ?function_handle);
Interactive verification passed.

Test the function name.

fh = 'sin';
verifyClass(testCase, fh, ?function_handle);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
verifyClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        char
    Expected Class:
        function_handle

Actual char:
    sin

Test fails.

Verify that a derived class is not the same class as its base class.

Create a class, BaseExample.

classdef BaseExample
end

Create a derived class, DerivedExample.

classdef DerivedExample < BaseExample
end

Verify the classes are not equal.

testCase = matlab.unittest.TestCase.forInteractiveUse;
verifyClass(testCase, DerivedExample(), ?BaseExample);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
verifyClass failed.
--> The value's class is incorrect.
    
    Actual Class:
        DerivedExample
    Expected Class:
        BaseExample

Actual DerivedExample:
      DerivedExample with no properties.

Test fails.

Use verifyClass to test the add5 function returns a double value.

Function for unit testing:

type add5.m
function res = add5(x)
% ADD5 Increment input by 5.
if ~isa(x,'numeric')
    error('add5:InputMustBeNumeric','Input must be numeric.')
end
res = x + 5;
end

TestCase class containing test methods:

type Add5Test.m
classdef Add5Test < matlab.unittest.TestCase
    methods (Test)
        function testDoubleOut(testCase)
            actOutput = add5(1);
            testCase.verifyClass(actOutput,'double')
        end
        function testNonNumericInput(testCase)
            testCase.verifyError(@()add5('0'),'add5:InputMustBeNumeric')
        end
    end
end

Create a test suite from the Add5Test class file.

suite = matlab.unittest.TestSuite.fromFile('Add5Test.m')
suite = 
  1x2 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

Run the tests.

result = run(suite);
Running Add5Test
..
Done Add5Test
__________

Tips

  • The method is functionally equivalent to the following methods:

    import matlab.unittest.constraints.IsOfClass;
    verifiable.verifyThat(actual, IsOfClass(className));
    verifiable.verifyThat(actual, IsOfClass(metaClass));
    

    There exists more functionality when using the IsOfClass constraint directly via verifyThat.

  • 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. Alternatively,

    • 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 test methods. A failure at the assertion point renders the current test method 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 mode is so fundamental that there is no point in continuing testing. These qualifications are also useful when fixture teardown does not restore the MATLAB state correctly and it is preferable to abort testing and start a fresh session. For more information, see matlab.unittest.qualifications.FatalAssertable.

Introduced in R2013a