Main Content

matlab.unittest.constraints.IsTrue 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint

测试值是否为 true

描述

matlab.unittest.constraints.IsTrue 类提供一个约束来测试值是否为 true。

创建对象

描述

示例

c = matlab.unittest.constraints.IsTrue 创建一个约束来测试值是否为 true。标量逻辑值 1 (true) 满足该约束。

示例

全部折叠

使用 IsTrue 约束测试值。

首先,导入此示例中使用的类。

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

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证 true 满足 IsTrue 约束。

testCase.verifyThat(true,IsTrue)
Verification passed.

测试 false 是否满足该约束。测试失败。

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

测试值 1。测试失败,因为该值的类型为 double

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

测试值 [true true]。测试失败,因为该值不是标量。

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

提示

  • IsTrue 的替代约束是 ReturnsTrue 约束。IsTrue 运行速度更快,更易于使用,但 ReturnsTrue 提供的诊断信息稍好。在此示例中,两个测试都失败,但第二个测试将函数句柄显示为诊断的一部分。

    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)

版本历史记录

在 R2013a 中推出