tags:

views:

17

answers:

2

I'm using MSTest and I want to write log entry before test executes and after it finishes. Obviously I don't want to add custom logging code at the beginning and end of each test - it would only make the test unreadable and seemed like a lot of effort (I have > 500 tests)

Using TestInitialize and TestCleanup seemed like the way to go but I can't get the test name.

Does anyone knows how to do this?

A: 

Update: Ok now I see what you're getting at. Bad news is I don't use MSTest or a MSTest fixture to find out..

In NUnit, you could

>"nunit-console.exe" API_Tests.dll /out:My.log /labels

THis outputs the following log file

***** Test.Gumba.API_Tests.Tests.ArithmeticProgression.DummyTest2
Woohoo! made it till test2
***** Test.Gumba.API_Tests.Tests.ArithmeticProgression.GeneratesTheRightProgressionAsSpecifiedByTheUser
Try#0 failed. due to 0. Retrying NUnit.Framework.AssertionException:   Expected is <System.Int32[10]>, actual is <System.Int32[0]>
<snipped>...

I was looking at the command line switches for MSTest and the following looks interesting

mstest /testcontainer:Some.dll /detail:testname

--------------- previous answer follows -----
To answer your question to the point, 'Execute around' methods can be done using a method that takes a delegate. However if you could elaborate on why you need this, maybe there is a better solution to achieve whatever it is you're after

e.g.

private void LogAround(Action action)
{
  // log entry with calling method name using StackTrace class
  action();
  // log exit
}

and calls would be

Do( delegate {
  // test code
});
Gishu
unfortunately that means that I would need to add this code to all of may present and future tests hurting their readability. I would also need to make sure all of the devs on the team use this in all their tests... I want a solution that "aromatically" adds logging to all of my tests
Dror Helper
@Dror - Updated answer.. You need some support from MSTest. Hope it exists ...
Gishu
+1  A: 

In MSTest, the name of the test case is available in the test context property. So to access it in the test initialize (as well as test cleanup) method, you can use something like this: -

    [TestInitialize()]
    public void MyTestInitialize() 
    {
        if (string.Equals(**TestContext.TestName**, "TestMethod1", StringComparison.OrdinalIgnoreCase))
        { 
        }
    }

Regards Aseem Bansal

Aseem Bansal