Main Content

matlab.unittest.constraints.IsTrue Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.Constraint

Test if value is true

Description

The matlab.unittest.constraints.IsTrue class provides a constraint to test if a value is true.

Creation

Description

example

c = matlab.unittest.constraints.IsTrue creates a constraint to test if a value is true. The constraint is satisfied by a logical scalar value of 1 (true).

Examples

collapse all

Test values using the IsTrue constraint.

First, import the classes used in this example.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsTrue

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Verify that true satisfies the IsTrue constraint.

testCase.verifyThat(true,IsTrue)
Verification passed.

Test if false satisfies the constraint. The test fails.

testCase.verifyThat(false,IsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsTrue failed.
    --> The value must evaluate to "true".
    
    Actual Value:
      logical
    
       0

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

testCase.verifyThat(1,IsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsTrue failed.
    --> The value must be logical. It is of type "double".
    
    Actual Value:
         1

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

testCase.verifyThat([true true],IsTrue)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsTrue failed.
    --> The value must be scalar. It has a size of [1  2].
    
    Actual Value:
      1×2 logical array
    
       1   1

Tips

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

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsTrue
    import matlab.unittest.constraints.ReturnsTrue
    
    testCase = TestCase.forInteractiveUse;
    actual = 1;
    expected = 2;
    testCase.verifyThat(isequal(actual,expected),IsTrue)
    testCase.verifyThat(@() isequal(actual,expected),ReturnsTrue)

Version History

Introduced in R2013a