matlab.unittest.qualifications.Verifiable.verifyThat - Verify value meets specified constraint - MATLAB (original) (raw)
Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications
Verify value meets specified constraint
Syntax
Description
verifyThat([testCase](#bt00riw-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fcf3d93fc-4930-4d51-8c76-ba57f62fe200),[constraint](#mw%5F0195c14d-228d-4eff-b10e-66100b515668))
verifies that actual
is a value that satisfies the specified constraint. If the testing framework displays diagnostic information for the test, it uses only the diagnostics provided by the constraint.
verifyThat([testCase](#bt00riw-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fcf3d93fc-4930-4d51-8c76-ba57f62fe200),[constraint](#mw%5F0195c14d-228d-4eff-b10e-66100b515668),[diagnostic](#mw%5F2dc3edd0-eff9-406c-994d-e10bab31a70a))
also associates the diagnostic information in diagnostic
with the qualification. When you use this syntax, the framework displays the diagnostic information provided by both constraint
and diagnostic
.
Input Arguments
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 amatlab.unittest.TestCase
object.
Value to test, specified as a value 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
Examples
Test if the actual value satisfies the specified constraint.
Create a test case for interactive testing.
testCase = matlab.unittest.TestCase.forInteractiveUse;
Test true
. Verify that it satisfies the IsTrue
constraint.
import matlab.unittest.constraints.IsTrue verifyThat(testCase,true,IsTrue)
Test if the strings "Hello"
and "hello"
are equal. This test fails because the comparison is case sensitive.
import matlab.unittest.constraints.IsEqualTo verifyThat(testCase,"Hello",IsEqualTo("hello"))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> StringComparator failed. --> The strings are not equal.
Actual Value:
"Hello"
Expected Value:
"hello"
------------------
Stack Information:
------------------
In C:\work\TestUsingConstraintsExample.m (TestUsingConstraintsExample) at 20
Test if a cell array containing an empty numeric array is empty. The test fails.
import matlab.unittest.constraints.IsEmpty verifyThat(testCase,{[]},IsEmpty,"Cell array must be empty.")
Verification failed. ---------------- Test Diagnostic: ---------------- Cell array must be empty. --------------------- Framework Diagnostic: --------------------- IsEmpty failed. --> The value must be empty. --> The value has a size of [1 1].
Actual Value:
1×1 cell array
{0×0 double}
------------------
Stack Information:
------------------
In C:\work\TestUsingConstraintsExample.m (TestUsingConstraintsExample) at 26
Verify that an array does not contain any NaN
values.
import matlab.unittest.constraints.HasNaN verifyThat(testCase,[Inf -7+1i],~HasNaN)
Test if a numeric array has two elements and both of its elements are greater than one.
import matlab.unittest.constraints.HasElementCount import matlab.unittest.constraints.IsGreaterThan verifyThat(testCase,[3 5],HasElementCount(2) & IsGreaterThan(1))
Tips
- 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
andIncomplete
. 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.
- 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
- To use
verifyThat
in a formal equivalence test for generated C/C++ code, specify actual as an instance of matlabtest.coder.MATLABCoderTester (MATLAB Test) and specify constraint as an instance of matlabtest.constraints.ExecutionMatchesMATLAB (MATLAB Test).
Version History
Introduced in R2013a