1

I am fairly new in using MATLAB unittesting framework. I don't know how to debug uncaught errors there. I am looking for a way similar to using dbstop if error functionality in MATLAB scripts which would stop just at the line c=(1:5)+randn(3) in the script.

My question is how to do it in the following...

classdef sample_Test < matlab.unittest.TestCase

    methods(Test)
        function sampleAATest(testCase)

            a=1;
            b=3; 
            c=(1:5)+randn(3)
        end
    end
end

any help is appreciated as this feature seems to be quite useful in complicated tests

4
  • if an error is thrown somewhere else in other code you cannot intercept it at the line you want. But if you suspect this line has a potential for error you can Set Conditional Breakpoints. They allow you to pause execution (still keeping the full stack and memory at this stage) if an expression of your choice is true. Commented Aug 6, 2015 at 21:33
  • thanks Hoki. The way I am using it seems not working for the example above. I set a breakpoint at line c and put the condition: b==3; here is what i put in the command line: close all;clc; clear classes; run(sample_Test). It actually doesn't stop Commented Aug 6, 2015 at 21:48
  • are you sure the code goes through there at all? If you place a hard breakpoint does it stops ? (and look at what does the workspace look like if it stops) Commented Aug 6, 2015 at 22:13
  • the code goes through since it is written in the unittest framework, however the test is unfinished. i guess the reason that I couldn't use breakpoints was because i used "clear classes" thanks to Andy for mentioning that Commented Aug 7, 2015 at 14:21

1 Answer 1

3

While this doesn't stop at the line in question, the StopOnFailuresPlugin can help you do this for other test failure types:

>> import matlab.unittest.*
>> import matlab.unittest.plugins.*
>>
>> suite = TestSuite.fromClass(?sample_Test)
>> runner = TestRunner.withTextOutput;
>> runner.addPlugin(StopOnFailuresPlugin);
>> runner.run(suite)

For uncaught exceptions it only catches the value after you have already exited the test method, which may not be as useful for you depending on your needs.

EDIT For more recent releases you can do this pretty easily using the 'Debug' option of runtests:

runtests('sample_Test', 'Debug', true)

/EDIT

Another way to do this for errors would be to issue

>> dbstop if all error

This will stop at the correct point and may work for this case but also stops at all caught errors. Sometimes this may be many errors and can take a while to get to the error you are interested in.

Finally, Hoki's suggestion in the comments is a good one. It is probably not working for you because clear classes also clears breakpoints.

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.