Consolidation For Microsoft Test And Microsoft Fakes

What’s new for MS Test?

The Unit Testing Framework (MSTest) describes Microsoft’s suite of unit testing tools as integrated into some versions of Visual Studio 2005 and later.

Similar to NUnit, MSTest is allowed to develop the Unit Test for your projects. From the NUnit 3.0 and latest MSTest will support to test Async methods, added platform support for Windows 10 and Metro application. However, the difference between MSTest and NUnit is MSTest is supported to test SSDT project also (Will discuss this feature in the separate topic).
Below are similarities between MSTest and NUnit syntax.

MSTest Attribute NUnit Attribute Purpose
[TestMethod] [Test] Identifies of an individual unit test
[TestClass] [TestFixture] Identifies of a class of unit tests, all Tests, and Initializations/Clean Ups must appear after this declaration.
[ClassInitialize] [TestFixtureSetUp] Identifies a method which should be called a single time prior to executing any test in the Test Class/Test Fixture
[ClassCleanup] [TestFixtureTearDown] Identifies a method in to be called a single time following the execution of the last test in a TestClass/TestFixture
[TestInitialize] [SetUp] Identifies a method to be executed each time before a TestMethod/Test is executed.
[TestCleanUp] [TearDown] Identifies a method to be executed each time after a TestMethod/Test has executed.
[AssemblyInitialize]  N/A Identifies a method to be called a single time upon before running any tests in a Test Assembly.
[AssemblyCleanUp]  N/A Identifies a method to be called a single time upon after running all tests in a Test Assembly.
[TestCategory] [Category] Identifies a group name of a test method. So the test methods can be grouped by group name.
[Ignore] [Ignore] Identifies that this unit test will be ignored when run the group of unit test methods.

Learn more about NUnit here and MSTest here.

What’s MS Fakes?

Fakes come in two flavors:

Stubs Shims
Create a fake object for an interface or class.
It can implement all properties and methods and an interface. However, only virtual properties and methods of a class can be overwritten.
Modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.
Performance Depend on the custom methods that have been implemented for testing purpose. Slower because they rewrite your code at run time.
Static methods, sealed types Cannot be used for static methods, non-virtual methods, sealed virtual methods, methods in sealed types, and so on. Can be used for all kinds of types.
Internal types Both stubs and shims can be used with internal types that are made accessible by using the assembly attribute InternalsVisibleToAttribute.
Private Method Can only replace visible methods. Can replaces calls to private methods if all the types on the method signature are visible
Interfaces and abstract methods. Stubs provide implementations of interfaces and abstract methods that can be used in testing Shims can’t instrument interfaces and abstract methods because they don’t have method bodies.

Below diagram shows you the functionality of each flavor on your unit testing behavior.

Fakes replace other components

 

What’s Difference between MSFakes and Moq?

Shims and Moq.

MSFakes is only available on Visual Studio to Premium or Ultimate (VS 2015 Enterprise).

Moq can create a dummy object for an interface or overwrite the virtual methods, properties of a class, the behavior of Moq and Stub is likely the same.

However, MS Fakes has a powerful flavor called shims. Shims can intercept the real methods of a class inside an Assembly and redirect the method-call to a method that has been defined as an overwritten in the shims context.

Below code is overwriting the get method of System.DateTime.Now property and return the fixed date whatever System.DateTime.Now is called.

 

Stubs and Moq.

Hopes, the comparison bellow will help to decide when the Stubs or Moq should be use.

 Stubs  Moq
Override methods to return hard-coded values also referred to as state-based.

Example: Your test class is depending on a method Calculate() taking 5 minutes to complete. Rather than wait for 5 minutes you can replace its real implementation with stub that returns hard-coded values; taking only a small fraction of the time.

Very similar to Stub but interaction-based rather than state-based. This means you don’t expect Mock to return some value, but to assume that specific order of method calls are made.

Example: You’re testing a user registration class. After calling Save, it should call SendConfirmationEmail.

 Stub is like making sure a method returns the correct value.  Moq is like actually stepping into the method and making sure everything inside is correct before returning the correct value.
 Stub: a dummy piece of code that lets the test run, but you don’t care what happens to it.  Mock: a dummy piece of code, that you VERIFY is called correctly as part of the test.

How to Fake an Assembly on your Unit Test Project?

In your unit test project right click to an assembly which you want to create a Fake and choose Add Fakes Assembly. Visual Studio will create a Fakes assembly and add to your test project reference as a photo below. Then you can use it as a normal assembly. The Fake classes will have prefix is Shim or Stub.

This topic is consolidated my understanding about MSTest and MSFakes. If there get some missing, please point it out. So this topic will have more values for those who want to start with MSTest, MSFakes and Moq.

Hope this helps.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *