How to check if a verification did not pass?

12 views (last 30 days)
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.
  2 Comments
Adam
Adam on 13 Apr 2017
If you want a result then just do a normal
result = isequal( value1, value2 );
rather than in the test framework.
Stephen Carter
Stephen Carter on 26 Apr 2017
Do you mind telling us what exactly your function will do with the values when the failure occurs?
Depending on your use case, there may be different solutions. One solution, although advanced, might be to add a listener on the testCase object immediately before your qualification.
For example:
addlistener(testCase,'VerificationFailed',@(~,qualEventData) yourOwnFunction(qualEventData));
See: https://www.mathworks.com/help/matlab/ref/matlab.unittest.testcase-class.html#bt74dv3-1 and https://www.mathworks.com/help/matlab/ref/matlab.unittest.qualifications.qualificationeventdata-class.html

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 13 Apr 2017
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.
  2 Comments
Steven Lord
Steven Lord on 20 Apr 2017
So I noticed that you accepted this Answer then unaccepted it, indicating that it didn't work for you. If you say a little about what you wanted that this didn't do, perhaps I can offer some additional suggestions.
Osama Omer Saeed
Osama Omer Saeed on 12 Nov 2021
I have a similiar question/problem to the one from @Jeno Boka. I would like to update a certain variable once a verification fails. I have tried to do that by writting my own function and then using the function handle in the diagnostic. something like below:
testcase.verifyThat(x, IsEqualTo(y), @myFunction);
However, this is not delivering the wanted result/behavior.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!