Coding⏱️ 1 min read📅 2026-05-31

How to Fix: How do I check "no exception occurred" in my MSTest unit test?

Understand how to write a unit test for a void method in C# that checks if no exception occurred.

Quick Answer: Use the 'ExpectedException' attribute on your test method, and set its value to false.

In MSTest unit tests, you can check if no exception occurred by using the Assert.DoesNotThrow method.

🔧 Proven Troubleshooting Steps

Method 1: Using Assert.DoesNotThrow

  1. Step 1: Replace Assert.IsTrue with Assert.DoesNotThrow.

Example Code

Assert.DoesNotThrow(() => { /* method to test */ });

🎯 Final Words

By using Assert.DoesNotThrow, you can verify that the method under test does not throw any exceptions, and your test will pass if no exception occurs.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions