Main Content

verifyTrue

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

Verify value is true

Description

example

verifyTrue(testCase,actual) verifies that the value of actual is logical 1 (true).

example

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

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. Although you can provide a value of any data type, the test fails if actual is not a logical scalar with a value of true.

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

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

verifyTrue(testCase,true)
Verification passed.

Test false.

verifyTrue(testCase,false)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyTrue failed.
    --> The value must evaluate to "true".
    
    Actual Value:
      logical
    
       0
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestMATLABLogicalFunctionsExample.m (TestMATLABLogicalFunctionsExample) at 16

When you test using verifyTrue, the test fails if the actual value is not of type logical.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test the value 1. The test fails because the value is of type double.

verifyTrue(testCase,1,"Value must be a logical scalar.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Value must be a logical scalar.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyTrue failed.
    --> The value must be logical. It is of type "double".
    
    Actual Value:
         1
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestANonzeroNumericValueExample.m (TestANonzeroNumericValueExample) at 14

When you test using verifyTrue, the test fails if the actual value is nonscalar.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Test the value [true true]. The test fails because the value is nonscalar.

verifyTrue(testCase,[true true])
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyTrue failed.
    --> The value must be scalar. It has a size of [1  2].
    
    Actual Value:
      1×2 logical array
    
       1   1
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestLogicalArraysExample.m (TestLogicalArraysExample) at 15

Tips

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

    import matlab.unittest.constraints.IsTrue
    testCase.verifyThat(actual,IsTrue)
    
  • verifyTrue might not provide the same level of strictness adhered to by other constraints such as IsEqualTo. In this example, the test using verifyTrue passes, but the test using verifyEqual fails.

    actual = 5;
    expected = uint8(5);
    testCase = matlab.unittest.TestCase.forInteractiveUse;
    verifyTrue(testCase,isequal(actual,expected))   % Test passes
    verifyEqual(testCase,actual,expected)   % Test fails
    

    In general, verifyTrue runs faster than IsEqualTo but is less strict and provides less diagnostic information in the event of a failure.

  • An alternative to verifyTrue is the verifyReturnsTrue method. verifyTrue runs faster and is easier to use, but verifyReturnsTrue provides slightly better diagnostic information. In this example, both tests fail, but the second test displays the function handle as part of the diagnostics.

    actual = 1;
    expected = 2;
    testCase = matlab.unittest.TestCase.forInteractiveUse;
    verifyTrue(testCase,isequal(actual,expected))
    verifyReturnsTrue(testCase,@()isequal(actual,expected))
  • 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.

Introduced in R2013a