# NUnit Extension Methods

DevFeed: [NUnit Extension Methods](<https://devfeed.tech/articles/nunit-extension-methods-33359.md>)

Original publisher: [Read original article](<https://timkellogg.me/blog/2011/02/26/nunit-extension-methods>)

Published: 2011-02-26T00:00:00Z

Content type: tutorial

Language: en

Sources: [Tim Kellogg](<https://devfeed.tech/sources/tim-kellogg.md>)

Topics: [NUnit](<https://devfeed.tech/topics/nunit.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Code](<https://devfeed.tech/topics/code.md>), [Framework](<https://devfeed.tech/topics/framework.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [framework](<https://devfeed.tech/tags/framework.md>), [nunit](<https://devfeed.tech/tags/nunit.md>), [object](<https://devfeed.tech/tags/object.md>), [static](<https://devfeed.tech/tags/static.md>), [testing](<https://devfeed.tech/tags/testing.md>), [unit-testing](<https://devfeed.tech/tags/unit-testing.md>)

## AI overview

This article presents NUnit extension methods that wrap common assertions such as Assert.AreEqual, Assert.IsNull, and Assert.IsNotNull. The methods provide a more readable style, including calls such as actual.ShouldBe(expected) and actual.ShouldBeNull().

## Source excerpt

I've always used NUnit for testing code so it's naturally the framework I'm most familiar with (I haven't used anything else). I learned unit testing using the classic Assert.AreEqual(expected, actual) methods. Although, I was finding my tests slightly confusing to read - I sometimes can't remember which comes first, expected or actual.More recently I've been getting into v2.5 including the new asserts - Assert.That(actual, Is.EqualTo(expected)). I think this makes a lot of sense and I often find myself using Assert.That most of the time just because it makes sense.Recently, a coworker created a few extension methods that I'm finding quite handy:public static void ShouldBe(this object @this, object expected) { Assert.AreEqual((dynamic)expected, (dynamic)@this);}public static void ShouldNotBe(this object @this, object expected) { Assert.AreNotEqual((dynamic)expected, (dynamic)@this);}public static void ShouldBeNull(this object @this) { Assert.IsNull(@this);}public static void ShouldNotBeNull(this object @this) { Assert.IsNotNull(@this);}I've completely fallen in love with how this reads: actual.ShouldBe(expected). It also makes me giggle to do actual.ShouldBeNull() (Don't you love extension methods?). This makes unit testing so easy...