MATLAB: How to check if a verification did not pass

class based testingtesting

I am currently developing a calss based Matlab Unit Test project, I wish to run a function, when two structs are not equal.Inside my testcase class I use the following method
verifyThat(testCase,value1,matlab.unittest.constraints.IsEqualTo(value2))
If the verfication fails I want to be able to run a function on those values. How can I get a return value from the verifyThat() function? Right after calling it, not at the end of the test suite.
result = verifyThat(testCase,value1,matlab.unittest.constraints.IsEqualTo(value2))
Gave me an error:
---------
Error ID:
---------
'MATLAB:TooManyOutputs'
--------------
Error Details:
--------------
Error using matlab.unittest.qualifications.Assertable/assertNotEmpty
Too many output arguments.

Best Answer

Consider adding a diagnostic in your qualification method call. For instance, here's an example that checks that the sin.m file is NOT a file with the extension .m on the MATLAB search path. Since exist('sin.m', 'file') should return 2 since that is a file with the extension .m on the path, this test will fail. When it does, the function handle I've specified as a diagnostic will be executed.
>> testCase = matlab.unittest.TestCase.forInteractiveUse;
>> verifyNotEqual(testCase, exist('sin.m', 'file'), 2, ...
@() fprintf('The sin.m file is located at %s.\n', which('sin.m')))
The result that is displayed in the Command Window is (I manually replaced my matlabroot by $MATLABROOT when I copied and pasted):
Interactive verification failed.
----------------
Test Diagnostic:
----------------
The sin.m file is located at $MATLABROOT\toolbox\matlab\elfun\sin.m.
---------------------
Framework Diagnostic:
---------------------
verifyNotEqual failed.
--> The values are equal using "isequaln".
Actual double:
2
Prohibited double:
2
If you want to display a message stored as a string or a char vector, or run a function handle, you can just specify the string, char vector, or function handle directly in your call like I did.
If you want to perform other types of diagnostics the link in the first sentence of my message includes a link to several diagnostic classes. You can instantiate one of those classes and pass the resulting object into the qualification method, just like I did with the function handle. For an example see the documentation for matlab.unittest.diagnostics.FigureDiagnostics. You could even build your own diagnostic class, specific for your application, by subclassing matlab.unittest.diagnostics.Diagnostic as shown in the example on its documentation page.