views:

67

answers:

1

MbUnit has a great attribute: MultipleCultureAttribute.

I am wondering if there is an easy way to do this in MSTest? So far, the best I can come up with is:

  • Externalizating the actual test code to a private method
  • Saving the current culture
  • Setting the culture and calling the private method (repeated for each culture)
  • And finally, reverting to the original culture

At best, it can be described as ugly ... and verbose.

+1  A: 

The simplest approach may be to use an anonymous delegate, however keep in mind that MSTest will treat this as a single test so it may be difficult to distinguish results for different cultures.

eg. Rough code for anonymous delegate approach.

public static class MultipleCultures
{
    public static void Do(Action action, params CultureInfo[] cultures)
    {
        CultureInfo originalCulture = Thread.CurrentCulture;

        try
        {
            foreach (CultureInfo culture in cultures)
            {
                Thread.CurrentCulture = culture;

                try
                {
                    action();
                }
                catch
                {
                    Console.WriteLine("Failed while running test with culture '{0}'.", culture.Name);
                    throw;
                }
            }
        }
        finally
        {
            Thread.CurrentCulture = originalCulture;
        }
    }
}

[TestClass]
public class Fixture
{
    [TestMethod]
    public void Test()
    {
        MultipleCultures.Do(() =>
        {
            // test code...
        }, CultureInfo.InvariantCulture, CultureInfo.GetCulture("en-GB"));
    }
}
Jeff Brown
This is definitely the correct answer. I have marked it so.But, as you can see from my profile, I am a new user and forgot some important information in the question. My company is a VB.NET shop and we will not get statement lamdas until VS2010.This is what I allude to in the question because it seems that I would need to put the actual test code into a private method and pass a function pointer to the Do() method. This creates a test disconnect, in my opinion.Is there anything else short of major work to extend the MSTest framework?
Eddie Butt
Unfortunately MSTest does not have an extensibility story for decorations like this. So your implementation choices are severely limited unless you change frameworks or languages.
Jeff Brown