Not pausing on error in unit test

Not pausing on error with debugger while running unit test. Please advise.

6 件のコメント

Pat Canny
Pat Canny 2022 年 7 月 14 日
Hi @Scott,
Can you provide any more details? What type of error, for instance? What type of test are you performing?
Any screenshots would help.
Otherwise, you may want to reach out to MathWorks Technical Support.
Thanks.
Scott
Scott 2022 年 7 月 14 日
It's a class based unit test. I place a breakpoint in one of the test cases at a function call. I run debugger, step into the function and I would like to pause on error (e.g. index exceeds number of elements in array), but the unit test runs completely and then shows error. Sorry, cannot show code.
Steven Lord
Steven Lord 2022 年 7 月 14 日
How are you running the tests? Are you using the button in the Editor, are you using runtests, are you building a test suite and calling run on it, or are you using some other approach?
Scott
Scott 2022 年 7 月 14 日
The run tests button in Editor
Rishita
Rishita 2022 年 7 月 15 日
close the current solution and reopen it.
Scott
Scott 2022 年 7 月 15 日
編集済み: Scott 2022 年 7 月 15 日
I am not clear on what you mean, e.g. exit MATLAB?

サインインしてコメントする。

回答 (3 件)

Jan
Jan 2022 年 7 月 15 日
編集済み: Adam Danz 2022 年 7 月 15 日

0 投票

If the errors are caught by TRY/CATCH blocks, you can stop Matlab and enter the debug mode by:
dbstop if caught error
Type this in the command window and start the test again.

1 件のコメント

Scott
Scott 2022 年 7 月 15 日
I am not using try/catch blocks. I am already in the debugger with "pause on errors" set. But the test runs to completion and then shows the error. I want it to pause on the error and stay in the debugger.

サインインしてコメントする。

Steven Lord
Steven Lord 2022 年 7 月 15 日

0 投票

I believe that the test infrastructure runs your tests effectively in a try / catch block and so an error breakpoint set to pause on errors will not be triggered.
Looking at the documentation for running tests in the Editor in various releases from release R2020b to the current release, it appears that the ability to specify the option to apply debugging capabilities when running tests from within the Editor was introduced in release R2022a. See the "Debug" row of the testing options table on this documentation page.
If you were running the test using runtests in the Command Window, that allows you to specify the name-value pair argument 'Debug' and tell MATLAB to enter debug mode when a test failure occurs in both releases R2020b and R2022a.
But now that I reread your question, are you asking how to stop and allow debugging when a test failure occurs, or are you asking how to stop and allow debugging when your test throws a hard error rather than a failure? In the latter case you may need to run this command before running your test:
dbstop if caught error
I caution you, though, that this may put you in the debugger in a file that is part of the testing infrastructure rather than the test file itself. To avoid this you may want to explicitly put a breakpoint in the test file on the line immediately before the error occurs and then begin your debugging process from that point, before the test actually reaches the line of code where the error is thrown.
Joris
Joris 2023 年 9 月 8 日
編集済み: Joris 2023 年 9 月 8 日

0 投票

During a unit test, the debugger will not stop on errors since everything is in a big try catch block because of the unit test.
You need to tell the debugger to 'Pause on Caught Errors'. But wait, that option is not available? It was a default option in the past and has disappeared. You need to manually set it back on again, see this link. Basically put this in your Command Window:
>> s = settings;
>> s.matlab.editor.displaysettings.ShowPauseOnCaughtErrorOption.PersonalValue = 1;
The option will appear again and presto, your unit test can be stopped right there and when the code crashes, not during the previous 999 loops that ran fine.

カテゴリ

製品

リリース

R2020b

質問済み:

2022 年 7 月 13 日

編集済み:

2023 年 9 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by