Replace MSTest to VSTest to support Fakes

I’ve created a unit test using MS Fakes Framework. When I tried to run it from the build script, I’ve got an error:
Test method DateTimeHelperTests.ConvertServerTimeToTimeZoneTimeTest threw exception:
Microsoft.QualityTools.Testing.Fakes.UnitTestIsolation.UnitTestIsolationException: Failed to resolve profiler path from COR_PROFILER_PATH and COR_PROFILER environment variables.
The answer of
http://stackoverflow.com/questions/26743342/running-a-test-using-shims-on-a-visual-studio-2013-test-agent told that instead of “using mstest.exe  I should have been using vstest.console.exe.”
There are a couple articles that explain the differences between MSTest and VSTest.console.exe.
I’ve created below table to describe parameters that should be changed if you move from MSTest to VSTest.
MSTestExe
VSTest.Console.exe
Comments
C:\Program Files (x86)\Microsoft Visual Studio 14\Common7\IDE\MSTest.exe
C:\Program Files (x86)\Microsoft Visual Studio 14\Common7\IDE\CommonExtensions\Microsoft\ TestWindow\VSTest.Console.exe
Full Path for typical installation
mstest /testcontainer:tests.dll
Vstest.console.exe myTestFile.dll myOtherTestFile.dll
Pass test.dll
/resultsfile:testResults.trx
/Logger:trx
TestResults\UserName_MachineName 2016-04-07 18_12_02.trx
/category:!Broken
/TestCaseFilter:”TestCategory!=Broken”
  /testsettings:Local.TestSettings
/Settings:[file name] e.g.
 /Settings:Local.RunSettings
mstest /testcontainer: “C:\TestProject2\myTestFile.dll” /testsettings:Local.TestSettings /category:!Broken /resultsfile:testResults.trx
vstest.console.exe  “C:\TestProject2\myTestFile.dll /Settings:Local.RunSettings /TestCaseFilter:”TestCategory=!Broken” /Logger:trx
Full example
 Tests.runsettings
<!– MSTest adapter –>
<MSTest>
<MapInconclusiveToFailed>True </MapInconclusiveToFailed>
<CaptureTraceOutput>false</CaptureTraceOutput>
<DeleteDeploymentDirectoryAfterTestRunIsComplete> False </DeleteDeploymentDirectoryAfterTestRunIsComplete>
<DeploymentEnabled>False</DeploymentEnabled>
</MSTest>
</RunSettings>
RunSettings for vstest.console adapter
/test:[test name]
to run multiple tests, use the /test option multiple times.
TestClass matches if the name Contains pattern
/Tests:[test name]
To provide multiple values, separate them by commas. Example: /Tests:TestMethod1,testMethod2
Classes and Methods matches if the name contains pattern
Also VSTest is running all tests in a single thread, so there is no proper isolation between tests and settings in one tests may effect subsequent tests.
I had to fix a few tests before the whole test project passed under VSTest.

#fakes, #unit-tests, #vstest

Global Convertion from MStest Assert to FluentAssertions

I found that FluentAssertions http://www.fluentassertions.com/ provide more descriptive logs in case of assertion failure.
However most of our test methods use MSTest Assert class.
For example statement
 Assert.IsTrue(string.IsNullOrEmpty(discountItemRecord.DiscountCode));
should  be changed to
(string.IsNullOrEmpty(discountItemRecord.DiscountCode)).Should().BeTrue();
They can be easily converted with the help of Visual Studio Replace. Just  select “use Regular Expressions”
For example
Search for:   \s*Assert.IsTrue\((.*)\);             replace to:      ($1).Should().BeTrue();
Search for:   \s*Assert.IsFalse\((.*)\);            replace to:      ($1).Should().BeFalse();
Search for:   \s*Assert.IsNotNull\((.*)\);        replace to:   ($1).Should().NotBeNull();
Search for:  (\s*)Assert.AreEqual\((.*),(.*)\);replace to:    $1($3).Should().Be($2);
I’ve used only most common asserts,that we are using,but they can be extended for other asserts.
Example of regular expression can be found on regex101

#fluentassertions, #regex, #unit-tests