15

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
Jader Dias
  • 88,211
  • 155
  • 421
  • 625

4 Answers4

9

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"
dcp
  • 54,410
  • 22
  • 144
  • 164
  • I don't use keyboard shortcuts for running tests, I use the mouse instead. I read the article and it only didn't help me since it is showing the different shortcuts for running with and without debugging. I want to debug but without stopping for expected exceptions. – Jader Dias Jul 29 '10 at 12:12
  • this will affect unexpected exceptions too – Jader Dias Jul 29 '10 at 14:26
4

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.

nmenezes
  • 910
  • 6
  • 12
2

@dcp's suggestion looks like it would work for MSTEST, but you might want to consider getting TestDriven.Net. I use it as a test runner almost exclusively and don't have this problem using the right-click "Run Tests in Debug" mode. In addition I find it to be more convenient than the built-in test runner in almost all circumstances.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

A recent 2022 article from Microsoft on shows this:

In the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.AccessViolationException. You can also select an entire category of exceptions.

Example picture from the same article: enter image description here

If this happens to work, I'll add an example to this answer as well.

DialFrost
  • 1,610
  • 1
  • 8
  • 28