필터 지우기
필터 지우기

How to check if a verification did not pass?

조회 수: 13 (최근 30일)
Jeno Boka
Jeno Boka 2017년 4월 13일
댓글: Osama Omer Saeed 2021년 11월 12일
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
Adam
Adam 2017년 4월 13일
If you want a result then just do a normal
result = isequal( value1, value2 );
rather than in the test framework.
Stephen Carter
Stephen Carter 2017년 4월 26일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2017년 4월 13일
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
Steven Lord
Steven Lord 2017년 4월 20일
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 2021년 11월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write Unit Tests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by