Main Content

verifyEmpty

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

Verify value is empty

Description

verifyEmpty(testCase,actual) verifies that actual is an empty MATLAB® array.

example

verifyEmpty(testCase,actual,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 an array 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.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;

Test an empty character vector. The test passes.

verifyEmpty(testCase,'')
Verification passed.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that an array with a zero dimension is empty.

verifyEmpty(testCase,ones(2,5,0,3))
Verification passed.

Test if the vector [2 3] is empty. The test fails.

verifyEmpty(testCase,[2 3],"Value must be empty.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Value must be empty.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEmpty failed.
    --> The value must be empty.
    --> The value has a size of [1  2].
    
    Actual Value:
         2     3
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForEmptyArraysExample.m (TestForEmptyArraysExample) at 10

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test an empty cell array. The test passes.

verifyEmpty(testCase,{})
Verification passed.

Test a cell array of empty numeric arrays. The test fails.

verifyEmpty(testCase,{[],[],[]})
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEmpty failed.
    --> The value must be empty.
    --> The value has a size of [1  3].
    
    Actual Value:
      1×3 cell array
    
        {0×0 double}    {0×0 double}    {0×0 double}
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForEmptyCellArraysExample.m (TestForEmptyCellArraysExample) at 10

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Create and test an empty test suite. The test passes.

emptyTestSuite = matlab.unittest.TestSuite.empty;
verifyEmpty(testCase,emptyTestSuite)
Verification passed.

Tips

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

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