Main Content

verifyEqual

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

Verify value is equal to specified value

Description

example

verifyEqual(testCase,actual,expected) verifies that actual is strictly equal to expected. If expected is not a MATLAB® or Java® object, actual and expected must have the same class, size, and value for the test to pass. verifyEqual compares actual and expected in the same way as the IsEqualTo constraint.

example

verifyEqual(testCase,actual,expected,diagnostic) also associates the diagnostic information in diagnostic with the qualification.

example

verifyEqual(___,Name,Value) verifies equality with additional options specified by one or more name-value arguments. Specify the name-value arguments after all of the arguments in any of the previous syntaxes. In R2021a and earlier, specify the name-value arguments before diagnostic.

Input Arguments

expand all

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

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

Expected value, specified as a value of any data type.

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

Depending on the test runner configuration, the testing framework might 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

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: verifyEqual(testCase,1.5,2,"AbsTol",1) verifies that the difference between the actual value 1.5 and the expected value 2 is within 1.

Absolute tolerance, specified as a numeric array. The sizes of AbsTol and expected must be the same or be compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For an absolute tolerance to be satisfied, abs(expected-actual) <= AbsTol must be true.

Relative tolerance, specified as a numeric array. The sizes of RelTol and expected must be the same or be compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For a relative tolerance to be satisfied, abs(expected-actual) <= RelTol.*abs(expected) must be true.

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Numeric values are equal if they are of the same class with equivalent size, complexity, and sparsity.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that a numeric value is equal to itself.

verifyEqual(testCase,5,5)
Verification passed.

Compare values of different sizes. The test fails.

verifyEqual(testCase,[5 5],5)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> Sizes do not match.
        
        Actual size:
             1     2
        Expected size:
             1     1
    
    Actual Value:
         5     5
    Expected Value:
         5
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareNumericValuesExample.m (CompareNumericValuesExample) at 18

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Compare two numeric values of different classes. The test fails.

verifyEqual(testCase,int8(5),int16(5),"Classes must match.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Classes must match.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> Classes do not match.
        
        Actual Class:
            int8
        Expected Class:
            int16
    
    Actual Value:
      int8
    
       5
    Expected Value:
      int16
    
       5
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareClassesExample.m (CompareClassesExample) at 12

Elements of equal cell arrays must match in class, size, and value.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Compare a cell array to itself. The test passes.

verifyEqual(testCase,{'cell',struct,5},{'cell',struct,5})
Verification passed.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test if the actual value 1.5 is equal to the expected value 2. The test fails.

verifyEqual(testCase,1.5,2)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error    RelativeError
            ______    ________    _____    _____________
        
             1.5         2        -0.5         -0.25    
    
    Actual Value:
       1.500000000000000
    Expected Value:
         2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingNumericTolerancesExample.m (CompareValuesUsingNumericTolerancesExample) at 13

Verify that the difference between the actual and expected values is within 1.

verifyEqual(testCase,1.5,2,"AbsTol",1)
Verification passed.

Test if the difference between the actual and expected values is less than 10%. The test fails.

verifyEqual(testCase,1.5,2, ...
    "Difference must be within relative tolerance.","RelTol",0.1)
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Difference must be within relative tolerance.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> The error was not within relative tolerance.
    --> Failure table:
            Actual    Expected    Error    RelativeError    RelativeTolerance
            ______    ________    _____    _____________    _________________
        
             1.5         2        -0.5         -0.25               0.1       
    
    Actual Value:
       1.500000000000000
    Expected Value:
         2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareValuesUsingNumericTolerancesExample.m (CompareValuesUsingNumericTolerancesExample) at 23

Tips

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

    import matlab.unittest.constraints.IsEqualTo
    testCase.verifyThat(actual,IsEqualTo(expected))

    Similarly, verifyEqual(testCase,actual,expected,"AbsTol",abstol,"RelTol",reltol) is functionally equivalent to the following code.

    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.AbsoluteTolerance
    import matlab.unittest.constraints.RelativeTolerance
    testCase.verifyThat(actual,IsEqualTo(expected, ...
        'Within',AbsoluteTolerance(abstol) | RelativeTolerance(reltol)))

    More functionality is available when using the IsEqualTo, AbsoluteTolerance, and RelativeTolerance constraints 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:

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

Compatibility Considerations

expand all

Not recommended starting in R2021b

Introduced in R2013a