matlab.unittest.qualifications.Verifiable.verifySubstring - Verify value contains specified string - MATLAB (original) (raw)

Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications

Verify value contains specified string

Syntax

Description

verifySubstring([testCase](#bt00rg2-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F3d4fa3c9-7ef5-4f1a-b2fd-6950ef3a234b),[substring](#mw%5Fae9074fa-f38d-4820-bfdc-ff75aba190de)) verifies that actual is a string scalar or character vector that contains substring.

example

verifySubstring([testCase](#bt00rg2-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F3d4fa3c9-7ef5-4f1a-b2fd-6950ef3a234b),[substring](#mw%5Fae9074fa-f38d-4820-bfdc-ff75aba190de),[diagnostic](#mw%5Fc4b9f69e-659c-4736-9bb9-0b7c64a02675)) also associates the diagnostic information in diagnostic with the qualification.

example

Input Arguments

expand all

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

Text that must be contained within the actual value, specified as a nonempty string scalar or character vector.

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

expand all

Test if the actual value contains the specified substring.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Define the actual value.

actual = "This is a long message.";

Verify that actual contains the text "long".

verifySubstring(testCase,actual,"long")

Show that case matters. This test fails because actual does not contain "Long".

verifySubstring(testCase,actual,"Long","Test is case sensitive.")

Verification failed. ---------------- Test Diagnostic: ---------------- Test is case sensitive. --------------------- Framework Diagnostic: --------------------- verifySubstring failed. --> The value does not contain the substring.

Actual Value:
    "This is a long message."
Expected Substring:
    "Long"
------------------
Stack Information:
------------------
In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 22

Show that the test fails if the substring is longer than the actual string.

verifySubstring(testCase,actual,"This is a long message with extra words.")

Verification failed. --------------------- Framework Diagnostic: --------------------- verifySubstring failed. --> The value does not contain the substring.

Actual Value:
    "This is a long message."
Expected Substring:
    "This is a long message with extra words."
------------------
Stack Information:
------------------
In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 27

Tips

Version History

Introduced in R2013a