Main Content

verifyLessThanOrEqual

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

Verify value is less than or equal to specified value

Description

example

verifyLessThanOrEqual(testCase,actual,ceiling) verifies that all elements of actual are less than or equal to all elements of ceiling.

example

verifyLessThanOrEqual(testCase,actual,ceiling,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 numeric array. The sizes of actual and ceiling must be the same or be compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

Maximum value, specified as a numeric array. The sizes of actual and ceiling must be the same or be compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

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 2 is less than or equal to 3.

verifyLessThanOrEqual(testCase,2,3)
Verification passed.

Verify that 3 is less than or equal to 3.

verifyLessThanOrEqual(testCase,3,3)
Verification passed.

Test if 9 is less than or equal to 5. The test fails.

verifyLessThanOrEqual(testCase,9,5)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyLessThanOrEqual failed.
    --> The value must be less than or equal to the maximum value.
    
    Actual Value:
         9
    Maximum Value (Inclusive):
         5
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareTwoNumbersExample.m (CompareTwoNumbersExample) at 20

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test if each element of the vector [5 2 7] is less than or equal to the ceiling value 7.

verifyLessThanOrEqual(testCase,[5 2 7],7)
Verification passed.

Test if each element of the matrix [1 2 3; 4 5 6] is less than or equal to the ceiling value 4.

verifyLessThanOrEqual(testCase,[1 2 3; 4 5 6],4, ...
    "All elements must be less than or equal to the ceiling value.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    All elements must be less than or equal to the ceiling value.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyLessThanOrEqual failed.
    --> Each element must be less than or equal to the maximum value.
        
        Failing Indices:
             4     6
    
    Actual Value:
         1     2     3
         4     5     6
    Maximum Value (Inclusive):
         4
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareArrayToScalarExample.m (CompareArrayToScalarExample) at 18

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test if each element of the array [5 -3 2] is less than or equal to each corresponding element of the ceiling array [5 -3 8].

verifyLessThanOrEqual(testCase,[5 -3 2],[5 -3 8])
Verification passed.

Compare an array to itself.

verifyLessThanOrEqual(testCase,eye(2),eye(2))
Verification passed.

Tips

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

    import matlab.unittest.constraints.IsLessThanOrEqualTo
    testCase.verifyThat(actual,IsLessThanOrEqualTo(ceiling))
  • 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