software testing
exception handling
test automation
error prevention
test strategies

Test with NO expected exception

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A test with no expected exception is usually just a normal passing test: the code should run and produce the expected result without throwing. The important idea is that "no exception" is not the real assertion by itself. The real assertion is usually about behavior, state, or output, with the absence of exceptions being part of normal success.

The Simplest Pattern

In most test frameworks, you do not need a special annotation to say "no exception expected." You simply call the code and assert the expected outcome.

csharp
1using Xunit;
2
3public class CalculatorTests
4{
5    [Fact]
6    public void Add_ReturnsSum()
7    {
8        var result = 2 + 3;
9        Assert.Equal(5, result);
10    }
11}

If the code throws unexpectedly, the test fails automatically. That is already enough in most cases.

Why Explicit “No Exception” Assertions Are Often Unnecessary

Some developers look for a special syntax to assert that nothing is thrown. Usually that is not the best first choice, because a well-written test should verify something more specific than mere survival.

A stronger test answers a meaningful question such as:

  • Did the function return the right value?
  • Did the object state change correctly?
  • Did the side effect happen?

If the code also throws, the test naturally fails.

When an Explicit Does-Not-Throw Check Can Help

There are cases where the whole point of the test is that a certain call path should complete without exception, especially after a bug fix.

For example:

csharp
1using Record = Xunit.Record;
2using Xunit;
3
4public class ParserTests
5{
6    [Fact]
7    public void Parse_ValidInput_DoesNotThrow()
8    {
9        var ex = Record.Exception(() => int.Parse("42"));
10        Assert.Null(ex);
11    }
12}

This is acceptable when the absence of an exception is the main regression condition you care about.

Prefer Behavior Assertions When Possible

Even in the previous example, the test is stronger if it also checks the result.

csharp
1using Xunit;
2
3public class ParserTests
4{
5    [Fact]
6    public void Parse_ValidInput_ReturnsExpectedValue()
7    {
8        var value = int.Parse("42");
9        Assert.Equal(42, value);
10    }
11}

Now the test says more than "it did not crash." It says what correct behavior actually is.

No Expected Exception Does Not Mean No Negative Testing

A healthy test suite still includes tests that intentionally verify exceptions for invalid input. The point is balance:

  • Valid input should usually be tested with normal success assertions.
  • Invalid input should be tested with explicit exception assertions where appropriate.

That way the suite documents both the supported path and the failure contract.

Regression Tests Often Fit This Pattern

A no-exception test is especially useful after fixing a crash bug. In that situation, the test documents that a specific input path should no longer throw, while stronger assertions can still be added around the resulting behavior.

Common Pitfalls

  • Treating "no exception" as a complete test even when meaningful output assertions are possible.
  • Looking for a framework-specific no-throw annotation when a normal test already expresses the requirement.
  • Forgetting that unexpected exceptions already fail the test by default.
  • Writing only positive-path tests and never verifying the actual exception behavior for invalid input.
  • Confusing regression protection with vague testing; a no-throw check should still tie to a real scenario.

Summary

  • In most test frameworks, you do not need special syntax for "no expected exception."
  • Just run the code and assert the correct result or state.
  • Unexpected exceptions already cause test failure automatically.
  • Explicit no-throw checks are useful only when the absence of an exception is itself the regression target.
  • The best tests verify behavior, not merely the lack of a crash.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.