Verify string matches specified regular expression
testCase— Test case object
Test case object, specified as an instance of the
matlab.unittest.qualifications.Verifiable class. Because the
matlab.unittest.qualifications.Verifiable and inherits its
testCase is typically a
actual— Value to test
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 string
scalar or character vector.
expression— Regular expression to match
Regular expression that the actual value must match, specified as a string scalar or character vector.
diagnostic— Diagnostic information to display
Diagnostic information to display when the qualification passes or fails, specified as a
string array, character array, function handle, or array of
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.
"My Custom Diagnostic"
To learn about attributes of methods, see Method Attributes.
Create a test case for interactive testing.
testCase = matlab.unittest.TestCase.forInteractiveUse;
Define the actual value.
actual = "Some Text";
Verify that the actual value matches
Show that case matters. The following test fails because the actual value does not start with a lowercase letter.
verifyMatches(testCase,actual,"^som","Test is case sensitive.")
Verification failed. ---------------- Test Diagnostic: ---------------- Test is case sensitive. --------------------- Framework Diagnostic: --------------------- verifyMatches failed. --> The value does not match the regular expression. Actual Value: "Some Text" Regular Expression: "^som" ------------------ Stack Information: ------------------ In C:\work\TestIfActualValueMatchesRegularExpressionExample.m (TestIfActualValueMatchesRegularExpressionExample) at 21
Define another regular expression. The
[Tt]? contained in the expression indicates that either
"t" matches at that location 0 times or 1 time.
expression = "Some [Tt]?ext";
Verify that the actual value matches the specified expression.
verifyMatches is a convenience method. For example,
verifyMatches(testCase,actual,expression) is functionally equivalent
to the following code.
import matlab.unittest.constraints.Matches testCase.verifyThat(actual,Matches(expression))
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
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
Incomplete. For more
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