Corey Coogan

Python, .Net, C#, ASP.NET MVC, Architecture and Design

  • Subscribe

  • Archives

  • Blog Stats

    • 47,177 hits
  • Meta

Initializing NHProf in MSTest

Posted by coreycoogan on September 4, 2010

Alternate title for this post could be Running initialization code before an MSTest Test Run

I’m not at all a fan of MSTest for various reasons. My preference is NUnit, however xUnit, mbUnit or any other framework is just as well. There are often cases when a client wants to use MSTest, usually for its Visual Studio integration. This is a compelling argument for a shop new TDD or unit testing, that wants the least amount of friction and doesn’t want to invest in TestDriven.Net or R#.

Integration tests written with MSTest are a great place to profile your NHibernate data access and catch any gotchas early in the development cycle. When the client is willing to pay for it, there’s no better tool than Ayende’s NHProf for this. NHProf can be easily initialized in code, but this should be done only once per test run, as you would bootstrap NHibernate itself only once. This led me on a search in MSTest for how to initialize once per test run. There’s widely known attributes for executing setup code when a test class fires and also whenever a test fires, but the answer to my requirement was found in the lesser known [AssemblyInitialize] attribute.

Just throw that attribute on a static void method that takes a TestContext argument and it will execute once before each test run. The class that houses the initialization method must also be decorated with the [TestClass] attribute.

[TestClass]
    public class AssemblyInit
    {
   
        [AssemblyInitialize]
        public static void Init(TestContext testContext)
        {
             HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }
    }

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.