Unit testing: Check result of e.g. verifyEqual

2 Ansichten (letzte 30 Tage)
Jeff Miller
Jeff Miller am 20 Aug. 2018
Kommentiert: Jeff Miller am 23 Aug. 2018
In a unit test, I have many lines such as:
verifyEqual(testCase,1.5,x);
Next I would like to have an 'if' statement to execute one of two code blocks depending on whether that verification was successful or failed. Does verifyEqual store its result somewhere that I can check, maybe in testCase, or must I duplicate the test on my own in order to check the result?
Honestly, I find it surprising that this is not supported:
thisResult = verifyEqual(testCase,1.5,x);
if ~thisResult % MATLAB complains here because verifyEqual does not return an output
disp('problem here'); % My goal is to set a debugger breakpoint on this line.
end
Any suggestions? Thanks

Akzeptierte Antwort

Steven Lord
Steven Lord am 20 Aug. 2018
For your specific use case of entering debug mode when a verification failure occurs, add the matlab.unittest.plugins.StopOnFailuresPlugin plugin to your test runner.
If you want to perform some other type of action when a verification failure occurs, you probably want to use a diagnostic in your verifyEqual call. Note that you can specify your diagnostic as a char vector or string and it will automatically be treated as a matlab.unittest.diagnostics.StringDiagnostic, and similarly for a function handle and a matlab.unittest.diagnostics.FunctionHandleDiagnostic.
  5 Kommentare
Steven Lord
Steven Lord am 22 Aug. 2018
In the scenario you described, you might want to tag your tests if you're using release R2015a or later. Doing so would allow you to run first all the tests for the constructor of the probability distribution class you're currently developing (which could have TestTags = {'constructor'}), then the tests for the density function (TestTags = {'density'}), etc.
This would let you lock down each method in turn before you move on to the next in "importance" and/or development order. It would also let you more quickly qualify bug fixes or enhancements to a given method by running just the tests specifically intended to test that method.
You can use this test tagging and selective running technique with the StopOfFailuresPlugin as well to iterate through just the failed tests for the density method and debug the failures.
Jeff Miller
Jeff Miller am 23 Aug. 2018
Thanks! I did not know about tags, and you are right that those will be very helpful in my scenario.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Run Unit Tests finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by