Converting a csproj into a test csproj#

Short answer.

It's as simple as adding one line to your csproj which in turn tells the IDE to treat that project like a test project.  Add the following line to your main PropertyGroup:

<ProjectGuid>{04082EBA-C85C-4336-B3FD-9891096BAA0F}</ProjectGuid>

Then just reference Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll and you are on your way.  Be sure to verify that Guid between releases. 

Long Answer.

When I start a project I create the base set of known assemblies that project will need.  I also add an additional test project which will contain any Scenario, Load and Stored Procedure tests.  Then I convert all assemblies to test projects.  I am a firm believer in Test Driven Development.  IMHO it is the only way to properly write code.  Having said that over the years I have developed my own set of TDD practices; one of which is the physical placement of your unit test classes in the assembly their testing.  Why? Well I will explain that in a future post call Unit Testing with Partial Classes.

With any decision there are of course trade-offs. In this case the trade-off is around the IDE interaction with hosting type projects, ie exe or web.  After you convert an exe to a test project, when you decide to run ( F5 ) it will actually fire off Test Manager rather than the launch app.  If you are a TDD bigot like me this is actually the correct action; if your not, I know your already complaining.  BUT all is not lost!  There is a little tool us TDD bigots keep in our back pocket at all times, TestDriven.Net.  This is an Visual Studio add-in and it allows you to execute your unit tests with just a right click.  If you choose to use this tool it isn't necessary to convert your project to a test project but rather just reference the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll.  This will allow you to fire your app as intended and run your tests with the same project configuration.

Disclaimer: this is not a Microsoft recommend practice.  You can find more information about the Visual Studio Team System Testing Framework here.

Saturday, January 21, 2006 8:36:27 AM (Central Standard Time, UTC-06:00) #    Comments [2]  | 

 

VSTS User Education Team Blog#

Rob Caron has relaunched the VSTS User Education Team Blog.  If you are working with TFS then you need this on your blogroll.  Be sure to check out the following three posts:

VSTS Backup and Restore Procedures
Team Foundation Server Permissions
Team Foundation Server Default Groups, Permissions, and Roles
Saturday, January 21, 2006 7:01:48 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Unit Testing with Anonymous Methods#

Mark Seemann recently published a great post demonstrating the use of anonymous methods in unit testing events.  Testing Events Using Anonymous Methods

That just rocks, thanks Mark!

Friday, January 20, 2006 7:14:39 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

VS 2002 / 2003 / 2005 Conversions and Building#
Friday, January 20, 2006 6:37:36 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

TFS Release Candidate is Coming#

Start getting ready, TFS RC is on it's way.  Thank you Jeff for the heads up!

Thursday, January 19, 2006 8:28:07 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Configuration Management in .NET 2.0#

Paulo Reichert posted a GREAT walkthrough / sample using the new Configuration Management features of .Net 2.0.

Wednesday, January 18, 2006 6:51:24 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Multiple Test Runs in one BuildType#

If you are using TeamBuild then this post is for you. 

In TeamExplorer when you create a new build type it will create a *.proj file.  During the creation of this build type, you will be asked which TestLists you would like executed in that build type.  Once completed if you open that proj file, you will see an item group that looks something like the following:

<ItemGroup>
  <MetaDataFile Include="$(SolutionRoot)\TestProject1\TestProject1.vsmdi">
    <TestList>TestList1;TestList2</TestList>
  </MetaDataFile>
</ItemGroup>

For this build type there are actually 2 different TestLists, TestList1 and TestList2.  These TestLists will get executed and reported on in the build.  With the out of box ItemGroup configuration both lists will actually roll up into one set of test results; but what if you wanted to to actually have multiple test runs for a given build. 

We can make a very slight modification to that generated code block and actually get a separate test run per test list.

<PropertyGroup>
  <VsmdiPath>$(SolutionRoot)\TestProject1\TestProject1.vsmdi</VsmdiPath>
</PropertyGroup>

<ItemGroup>
  <MetaDataFile Include="$(VsmdiPath)">
    <TestList>TestList1</TestList>
  </MetaDataFile>

  <MetaDataFile Include="$(VsmdiPath)">
    <TestList>TestList2</TestList>
  </MetaDataFile>
</ItemGroup>

More to come....

Tuesday, January 17, 2006 11:50:43 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

ExpectedException Exception Message Validation#

While you cannot validate you exception's message in the ExpectedException attribute all is not lost.  Lets walk through three scenarios.

In all three tests you will see an Assert.Fail in the try block.  You need this statement in case the target doesn't throw any exceptions, therefor failing the test.  The first example is a passing test, throw and exception with a message, catch it and compare the message string. 

   [TestMethod]
   public void PassingException()
   {
      const string expectedExceptionMessage = "Foobar Message";

      try
      {
         throw new ApplicationException( expectedExceptionMessage );
         Assert.Fail( "Expected an exception to get thrown by target" );
      }

      catch ( ApplicationException ex )
      {
         Assert.AreEqual<string>( expectedExceptionMessage, ex.Message );
      }
   }  

Here we see the same test failing only because the message contained in the exception is different.

   [TestMethod]
   public void FailingOnExceptionMessage()
   {
      const string expectedExceptionMessage = "Foobar Message";

      try
      {
         throw new ApplicationException( "Invalid" );
         Assert.Fail( "Expected an exception to get thrown by target" );
      }

      catch ( ApplicationException ex )
      {
         Assert.AreEqual<string>( expectedExceptionMessage, ex.Message );
      }
   }

While this last test follows the same pattern seen above the test is really only testing for the type of exception being throw.  This should be refactored to use the ExpectedException attribute.

   [TestMethod]
   public void FailingOnExceptionType()
   {
      const string expectedExceptionMessage = "Foobar Message";

      try
      {
         throw new Exception( expectedExceptionMessage );
         Assert.Fail( "Expected an exception to get thrown by target" );
      }

      catch ( ApplicationException ex )
      {     
      }

      catch
      {
         Assert.Fail( "Incorrect Exception type was thrown by target" );
      }
   }

Same test refactored, this test still fails but only to illustrate that example.

   [TestMethod, ExpectedException ( typeof ( ApplicationException) )]
   public void FailingOnExceptionType()
   {
      throw new Exception( "..." );
   }

Monday, January 16, 2006 9:27:52 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

http://blogs.msdn.com/csell#

It was finally time to join the long list of fellow Microsoft bloggers.  You will now find a subset of http://csell.net content @ http://blogs.msdn.com/csell.

Saturday, January 14, 2006 11:50:44 AM (Central Standard Time, UTC-06:00) #    Comments [1]  | 

 

Virtual PC's Differencing Disks#

I love Virtual PC, and I use if for all of my development.  I have a ton of VPC's just sucking up MAJOR disk space. I ran across and article from Andrew ConnellHOWTO: Use Virtual PC's Differencing Disks to your Advantage.  I wish I would have known about that feature a few years ago...

Friday, January 13, 2006 11:35:51 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

All content © 2010, Clark Sell
On this page
This site
Calendar
<January 2006>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: