matlab.unittest.qualifications.Verifiable.verifyWarning - Verify function issues specified warning - MATLAB (original) (raw)
Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications
Verify function issues specified warning
Syntax
Description
verifyWarning([testCase](#bt00roo-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fb1ce44df-d09e-42b7-9bb8-f0d168b65ec1),[identifier](#mw%5Fa3ce156a-58ee-4bf4-9b1d-03ee110f7957))
verifies that actual
is a function handle that issues the warning specified by identifier
.
verifyWarning([testCase](#bt00roo-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5Fb1ce44df-d09e-42b7-9bb8-f0d168b65ec1),[identifier](#mw%5Fa3ce156a-58ee-4bf4-9b1d-03ee110f7957),[diagnostic](#mw%5F30c0a0d8-0d17-4ef1-b8e9-da0d6a518dd8))
also associates the diagnostic information in diagnostic
with the qualification.
`[output1,...,outputN]` = verifyWarning(___)
also returns any outputs produced when the function handle is invoked. You can use any of the input argument combinations in the previous syntaxes.
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. Although you can provide a value of any data type, the test fails if actual
is not a function handle.
Example: @() myFunction(1,2)
Example: @() mkdir("myFolder")
Warning identifier, specified as a character vector, cell array of character vectors, or string array.
Example: "MATLAB:MKDIR:DirectoryExists"
Example: ["MyComponent:FirstID" "MyComponent:SecondID"]
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
Verify that the mkdir
function issues a specified warning if it is used to create a folder that already exists.
This example assumes that your current folder has a subfolder named myFolder
. Create the subfolder if it does not exist.
if ~isfolder("myFolder") mkdir myFolder end
If you try to create myFolder
again, mkdir
issues a warning. Return the warning identifier.
Warning: Directory already exists.
[~,identifier] = lastwarn
identifier = 'MATLAB:MKDIR:DirectoryExists'
Now, create a test case for interactive testing. Verify that if mkdir
is called to create an existing folder, it warns and the warning has the identifier "MATLAB:MKDIR:DirectoryExists"
.
testCase = matlab.unittest.TestCase.forInteractiveUse; verifyWarning(testCase,@() mkdir("myFolder"),"MATLAB:MKDIR:DirectoryExists")
Test if the actual value is a function handle that issues a specified warning.
Create a test case for interactive testing.
testCase = matlab.unittest.TestCase.forInteractiveUse;
Verify that the warning
function issues a warning with the expected identifier.
verifyWarning(testCase,@() warning("SOME⚠️id","Warning!"), ... "SOME⚠️id")
Repeat the test with "OTHER⚠️id" as the expected warning identifier. The test fails.
verifyWarning(testCase,@() warning("SOME⚠️id","Warning!"), ... "OTHER⚠️id","Warning identifiers must match.")
Warning: Warning! Verification failed. ---------------- Test Diagnostic: ---------------- Warning identifiers must match. --------------------- Framework Diagnostic: --------------------- verifyWarning failed. --> The function handle did not issue the expected warning(s).
Actual Warning(s):
--> 'SOME⚠️id'
Expected Warning(s):
--> 'OTHER⚠️id'
Evaluated Function:
function_handle with value:
@()warning("SOME⚠️id","Warning!")
------------------
Stack Information:
------------------
In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 21
Test the rand
function, and also examine the output of the function. The test fails because rand
does not issue any warnings.
r = verifyWarning(testCase,@rand,"SOME⚠️id")
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyWarning failed. --> The function handle did not issue any warnings.
Expected Warning(s):
--> 'SOME⚠️id'
Evaluated Function:
function_handle with value:
@rand
------------------
Stack Information:
------------------
In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 27
r =
0.8147
Verify that the test fails if the actual value is not a function handle.
verifyWarning(testCase,5,"SOME⚠️id")
Verification failed. --------------------- Framework Diagnostic: --------------------- verifyWarning failed. --> The value must be an instance of the expected type.
Actual Class:
double
Expected Type:
function_handle
Actual Value:
5
------------------
Stack Information:
------------------
In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 31
Tips
verifyWarning
is a convenience method. For example,verifyWarning(testCase,actual,identifier)
is functionally equivalent to the following code.
import matlab.unittest.constraints.IssuesWarnings
testCase.verifyThat(actual,IssuesWarnings(cellstr(identifier)))
More functionality is available when using the IssuesWarnings constraint directly via verifyThat.- 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
Version History
Introduced in R2013a