NUnit's Assert.Equals throws exception Assert.Equals should not be used for assertions
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In NUnit, Assert.Equals is a trap for people expecting it to behave like a real test assertion. It is not the same as Assert.AreEqual or Assert.That, and NUnit actively throws an exception to stop you from misusing it. The fix is simple: use one of NUnit’s actual assertion APIs and treat Assert.Equals as off-limits in tests.
Why Assert.Equals Fails
Assert.Equals comes from System.Object. It is not NUnit’s equality assertion API. Because this mistake was so common, NUnit explicitly throws when developers try to use it as an assertion.
Bad example:
That code does not express a real NUnit test assertion and will trigger the message saying Assert.Equals should not be used for assertions.
Use Assert.AreEqual for Direct Equality Checks
If you want a straightforward equality assertion, use Assert.AreEqual.
This is the classic NUnit style and works well for simple value comparisons.
Prefer Assert.That for Readable, Extensible Assertions
Modern NUnit code often prefers the constraint model with Assert.That.
This style scales better when assertions become more expressive, such as ranges, collection properties, or string matching.
For example:
That is one reason many teams standardize on Assert.That.
Know the Difference Between Equality Methods
There are several related methods with different meanings:
- '
Assert.AreEqual(expected, actual)checks value equality' - '
Assert.AreSame(expected, actual)checks object identity' - '
Assert.AreNotEqual(expected, actual)checks inequality' - '
Assert.That(actual, Is.EqualTo(expected))checks equality through the constraint model'
Use the method that matches what you are actually testing.
For example, reference identity is not the same as equal content:
Why NUnit Chooses to Throw
The exception is deliberate. If NUnit silently allowed Assert.Equals, many tests would appear meaningful while actually using the wrong method semantics. Failing fast is better than letting misleading tests pass through code review.
This is one of those cases where the framework is protecting you from a subtle but common API confusion.
Refactor Old Tests Safely
If you inherit a codebase containing Assert.Equals, replace those calls systematically.
A simple rule:
- if the intention is equality, convert to
Assert.AreEqualorAssert.That(..., Is.EqualTo(...)) - if the intention is identity, convert to
Assert.AreSame
Example refactor:
After refactoring, run the test suite because some mistaken uses of Assert.Equals may have hidden incorrect expectations.
Team Style Guidance
If your team is deciding between Assert.AreEqual and Assert.That, the important part is consistency. Both are valid, but mixed styles inside the same test class can make code harder to scan.
Many teams choose:
- '
Assert.Thatfor new code' - '
Assert.AreEqualonly in older tests or simple direct comparisons'
Whatever you choose, ban Assert.Equals in review.
Common Pitfalls
One common mistake is assuming any method named Equals inside Assert must be a real NUnit assertion. It is not.
Another issue is using AreSame when the test really wants value equality. That produces brittle tests for strings, DTOs, and value objects.
A third mistake is doing broad search-and-replace without understanding whether each failing test intended equality or identity.
Summary
- '
Assert.Equalsis not a valid NUnit assertion method.' - Use
Assert.AreEqualorAssert.That(..., Is.EqualTo(...))for value equality. - Use
Assert.AreSameonly when object identity is the goal. - NUnit throws intentionally to prevent misleading tests.
- If you see
Assert.Equalsin a test suite, replace it as part of cleanup.
Related reading
- One-time initialization for NUnit
- One failing test causes other async tests to fail
- Options or alternatives to Testcontainers for Spring Boot integration testing in Kubernetes?
- Override a property for a single Spring Boot test
- Override a single Configuration class on every spring boot Test
- Override default Spring-Boot application.properties settings in Junit Test
- Override default Spring-Boot application.properties settings in Junit Test
- Override Java System.currentTimeMillis for testing time sensitive code
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.