BLOG

img
5 Minute read

MS-Test Framework Cheat Sheet

MSTest is one of the Visual Studio in-built unit testing frameworks which allows developers to test the code without using any third-party frameworks/plugins. Using MSTest framework, developers can write effective unit tests to test software applications.

This framework has several options that developers can use to customize test run. Since the MSTest framework ships with Visual Studio, developers who use the Visual Studio IDE for development & testing prefer the MSTest framework over other test frameworks.

MSTest Unit Testing framework is among the most popular frameworks in the .NET world. However, there is hardly any place where one can find details about its most used classes and methods, all compiled in one place.

MS-Test Framework - Cheat Sheet


Class
Method
Use
Assert AreEqual(Object, Object) Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.
AreNotEqual(Object, Object) Tests whether the specified objects are unequal and throws an exception if the two objects are equal. Different numeric types are treated as unequal even if the logical values are equal. 42L is not equal to 42.
AreNotSame(Object, Object) Tests whether the specified objects refer to different objects and throws an exception if the two inputs refer to the same object.
AreSame(Object, Object) Tests whether the specified objects both refer to the same object and throws an exception if the two inputs do not refer to the same object.
Equals(Object, Object) Static equal’s overloads are used for comparing instances of two types for reference equality. This method should not be used for comparison of two instances for equality. This object will always throw with Assert.Fail. Please use Assert.AreEqual and associated overloads in your unit tests.
Fail() Throws an AssertFailedException.
Inconclusive() Throws an AssertInconclusiveException.
IsFalse(Boolean) Tests whether the specified condition is false and throws an exception if the condition is true.
IsInstanceOfType(Object, Type) Tests whether the specified object is an instance of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of the object.
IsNotInstanceOfType(Object, Type) Tests whether the specified object is not an instance of the wrong type and throws an exception if the specified type is in the inheritance hierarchy of the object.
IsNotNull(Object) Tests whether the specified object is non-null and throws an exception if it is null.
IsNull(Object) Tests whether the specified object is null and throws an exception if it is not.
IsTrue(Boolean) Tests whether the specified condition is true and throws an exception if the condition is false.
ReplaceNullChars(String) Replaces null characters ('\0') with "\0".
ThrowsException(Action) Tests whether the code specified by delegate action throws exact given exception of type T (and not of derived type) and throws AssertFailedException if code does not throws exception or throws exception of type other than T.
ThrowsExceptionAsync(Func) Tests whether the code specified by delegate action throws exact given exception of type T (and not of derived type) and throws AssertFailedException if code does not throws exception or throws exception of type other than T.
CollectionAssert AllItemsAreInstancesOfType(ICollection, Type) Tests whether all elements in the specified collection are instances of the expected type and throws an exception if the expected type is not in the inheritance hierarchy of one or more of the elements.
AllItemsAreNotNull(ICollection) Tests whether all items in the specified collection are non-null and throws an exception if any element is null.
AllItemsAreUnique(ICollection) Tests whether all items in the specified collection are unique or not and throws if any two elements in the collection are equal.
AreEqual(ICollection, ICollection) Tests whether the specified collections are equal and throws an exception if the two collections are not equal. Equality is defined as having the same elements in the same order and quantity. Whether two elements are the same is checked using Equals(Object, Object) method. Different references to the same value are considered equal.
AreEquivalent(ICollection, ICollection) Tests whether two collections contain the same elements and throws an exception if either collection contains an element not in the other collection.
AreNotEqual(ICollection, ICollection) Tests whether the specified collections are unequal and throws an exception if the two collections are equal. Equality is defined as having the same elements in the same order and quantity. Whether two elements are the same is checked using Equals(Object, Object) method. Different references to the same value are considered equal.
AreNotEquivalent(ICollection, ICollection) Tests whether two collections contain the different elements and throws an exception if the two collections contain identical elements without regard to order.
Contains(ICollection, Object) Tests whether the specified collection contains the specified element and throws an exception if the element is not in the collection.
DoesNotContain(ICollection, Object) Tests whether the specified collection does not contain the specified element and throws an exception if the element is in the collection.
IsNotSubsetOf(ICollection, ICollection) Tests whether one collection is not a subset of another collection and throws an exception if all elements in the subset are also in the superset.
IsSubsetOf(ICollection, ICollection) Tests whether one collection is a subset of another collection and throws an exception if any element in the subset is not also in the superset.
StringAssert Contains(String, String) Tests whether the specified string contains the specified substring and throws an exception if the substring does not occur within the test string.
DoesNotMatch(String, Regex) Tests whether the specified string does not match a regular expression and throws an exception if the string matches the expression.
EndsWith(String, String) Tests whether the specified string ends with the specified substring and throws an exception if the test string does not end with the substring.
Matches(String, Regex) Tests whether the specified string matches a regular expression and throws an exception if the string does not match the expression.
StartsWith(String, String) Tests whether the specified string begins with the specified substring and throws an exception if the test string does not start with the substring.

The case of ArgumentNullException

For testing if the method throws ArgumentNullException, we can use ExpectedException[typeof(ArgumentNullException)] parameter as shown below in the example for validation:

Code to be tested:

  1. public void HowToTestArgumentNullException(object sender)
  2. {   
  3.      Assert.ArgumentNotNull(sender, “sender”);   
  4.      //Your code here   
  5. }  

Test Method:

  1. [TestMethod]
  2. [ExpectedException(typeof(ArgumentNullException))]
  3. public void Validate_ArgumentNullException_SenderAsNull(object sender)
  4. {   
  5.      //Arrange      
  6.      //Act      
  7.      //Assert code here      
  8. }  

In case you want to learn more MSTest Unit Testing framework, feel free to reach out to us at marketing@altudo.co and we can have a detailed discussion, along with implementation plan that works for you.

Rajat Jhamb


Talk to our Experts

Talk to us about how we bring together 1:1 personalisation, deep Martech Expertise, CX & Demand Gen Strategy, Engagement Analytics & Cross-Channel Orchestration to drive award winning experiences that convert

Get in touch for a complimentary consultation or a demo today.

Expert Workshops

Free workshops, expert advice & demos- to help your realize value with Sitecore

Register

Session Presentations

  • Sitecore + SFMC= Marketing Success
  • Transforming The Future Of eCommerce
Meet Us

Giveaways:

Participate in our event survey , meet us at our booth , get free giveaways & a chance to win an iPhone 11

Let’s go
Close Button