Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent expected exceptions from breaking the debug test run?

When running MSTEST unit tests in debug mode, the execution stops in every expected exception that is thrown. My test looks like this

[TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowExceptionWhenPassingNull()
{
    object data = null;
    target.CheckNull(data);
}

the target method looks like this:

public void CheckNull(object data)
{
    if (ReferenceEquals(null, data))
    {
        throw new ArgumentNullException("data");
    }
} // test run breaks here: ArgumentNullException was unhandled by user code
like image 945
Jader Dias Avatar asked Jul 29 '10 12:07

Jader Dias


Video Answer


2 Answers

Did you try running the tests using ctrl-R ctrl-T instead of ctrl-R T?

EDIT If it's not a keyboard shortcut issue, then check out this link. You could try the following as noted there:

  1. Disable "break on user unhandled exceptions" for the exception types you you are encountering here (via Debug -> Exceptions)
  2. Disable "break on user unhandled exceptions" for all exceptions (via Debug -> Exceptions)
  3. Disable "Just My Code"
like image 107
dcp Avatar answered Sep 30 '22 17:09

dcp


CTRL + R A works for me without changing any option.

I think the problem you have is because you are running the test project from the Start Debugging (F5) button on the toolbar. If you click the green play button, you will stop in every exception, even expected ones.

To run all tests without stopping in every exception, click on: Test -> Run -> All Tests in Solution or use the shortcut: CTRL + R, A

With the test results window open, CTRL + R, D also works. In the test results window it becomes clear the difference between Run Tests and Debug tests.

like image 22
nmenezes Avatar answered Sep 30 '22 16:09

nmenezes