Skip to content

Instantly share code, notes, and snippets.

@ssg
Last active March 13, 2017 08:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ssg/5329684 to your computer and use it in GitHub Desktop.
Save ssg/5329684 to your computer and use it in GitHub Desktop.
MBUnit <-> NUnit migration cheat sheet.

This is a cheat sheet I prepared when testing migrations between MBUnit and NUnit. It only covers the areas we used so it's far from complete. It should give a good head start though.

MBUnit NUnit
[FixtureSetUp] [OneTimeSetUp]
[Row] [TestCase]
[Factory] [TestCaseSource] for test cases, [ValueSource] for individual parameters
[Column] [Values]
[SequentialJoin] [Sequential]
[CombinatorialJoin] [Combinatorial]
Assert.IsInstanceOfType<T>(y) Assert.IsInstanceOf<T>(y)
Assert.AreElementsEqual(coll, expectedColl) CollectionAssert.AreEqual(expectedColl, coll)
Assert.AreElementsEqualIgnoringOrder(coll, expectedColl) CollectionAssert.AreEquivalent(expectedColl, coll)
Assert.AreElementsSame(expectedColl, coll) CollectionAssert.AreEqual(expectedColl, coll) ???
Assert.AreEqual(coll, expectedColl) CollectionAssert.AreEqual(expectedColl, coll)
Assert.Contains(coll,item) CollectionAssert.Contains(coll, item)
Assert.DoesNotContain(coll,item) CollectionAssert.DoesNotContain(coll, item)
Assert.IsEmpty(coll) CollectionAssert.IsEmpty(coll)
Assert.IsEmpty(str) Assert.That(str, Is.Empty)
Assert.StartsWith(str, substr) StringAssert.StartsWith(substr, str)
Assert.EndsWith(str, substr) StringAssert.EndsWith(substr, str)
Assert.Contains(str, substr) StringAssert.Contains(substr, str)
Assert.DoesNotContain(str, substr) StringAssert.DoesNotContain(substr, str)
Assert.AreEqual(expected, result, new StructuralEqualityComparer) Assert.That(result, Is.EqualTo(expected).Using(new Comparer())
Assert.LessThanOrEqualTo(x,y) Assert.LessOrEqual(x, y)
Assert.LessThan(x,y) Assert.Less(x, y)
Assert.GreaterThan(x,y) Assert.Greater(x, y)
Assert.GreaterThanOrEqualTo(x,y) Assert.GreaterOrEqual(x, y)
Assert.ForAll(coll, item => code) Assert.IsFalse(coll.Any(item => !code))
Assert.Between(modifiedDate, new DateTime(2014, 01, 01), new DateTime(2999, 01, 01)) Assert.That(modifiedDate, Is.InRange(new DateTime(2014, 01, 01), new DateTime(2999, 01, 01)))
Assert.AreApproximatelyEqual(expectedDate, actualDate, TimeSpan.FromSeconds(5)) Assert.That(actualDate, Is.EqualTo(expectedDate).Within(5).Seconds);
@nickbeaugie
Copy link

Great reference! Some extra ones I used when doing the same exercise:

Note I have a different translation of Assert.AreElementsEqual. CollectionAssert.AreEquivalent enforces the order of a collection while CollectionAssert.AreEqual is not order sensitive.

MBUnit NUnit
[AssertException(typeof(Exception))] [ExpectedException(typeof(Exception))]
[FixtureSetUp] [TestFixtureSetUp]
[ExpectedArgumentException] [ExpectedException(typeof(ArgumentException))]
Assert.AreElementsEqual(coll, expectedColl) CollectionAssert.AreEqual(coll, expectedColl)
Assert.AreElementsEqualIgnoringOrder(coll, expectedColl) CollectionAssert.AreEquivalent(coll, expectedColl)
Assert.IsInstanceOfType<T>(x) Assert.IsInstanceOf<T>(x)
Assert.Between(modifiedDate, new DateTime(2014, 01, 01), new DateTime(2999, 01, 01)) Assert.That(modifiedDate, Is.InRange(new DateTime(2014, 01, 01), new DateTime(2999, 01, 01)))
Assert.AreApproximatelyEqual(expected.CreatedDate.ToUniversalTime(), actual.CreatedDate, new TimeSpan(0, 0, 0, 5)) Assert.That(actual.CreatedDate, Is.EqualTo(expected.CreatedDate.ToUniversalTime()).Within(5).Seconds);

@henryebwhite
Copy link

Very useful resource, thank you!
Am I right in thinking there's no NUnit equivalent of AssertMultiple?
Also for Assert.AreApproximatelyEqual I used two asserts, one Greater and one Lesser. Your method (nickbeaugie) is quite a bit neater.

I'll add any new ones I find throughout my conversion task, although it's a pretty complete list.

@ssg
Copy link
Author

ssg commented Jan 29, 2016

thanks folks. i updated some of these.

@ssg
Copy link
Author

ssg commented Jan 29, 2016

@nickbeaugie ExpectedException was deprecated in NUnit 3 IIRC fyi.

@outring
Copy link

outring commented Jun 22, 2016

Assert.ForAllAssert.That(collection, Has.All.Matches<SomeType>(item => code))
Assert.ExistsAssert.That(collection, Has.Some.Matches<SomeType>(item => code))
Assert.DoesNotExistAssert.That(collection, Has.None.Matches<SomeType>(item => code))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment