azure-devopsazure-pipelinesregionvstestcurrentculture

Is there a way to change the default thread culture in visual studio team service


I have a VSTS account within the region of US. And I have a bunch of Unit tests, which run successfully on my local server. But when deployed to the VSTS, all the tests related to the date time are failed. I guess this is because the interpretation of the date time format is different as my local is using the UK format. Because there are quite a number of test projects, instead of changing it individually, is there any way to change the VSTS default thread's current culture as UK? I know probably the change of region might do the trick, but any other way?


Solution

  • Of course, a lot is possible, but it would be a far better idea to make sure your application correctly handles accidentally being deployed to a machine with a different culture.

    You've basically uncovered an unexpected and unwanted behaviour of your code, or you've detected that you're not able to run in different cultures.

    I'd personally try to make the code more resilient, e.g. explicitly set the right culture and depend on InvariantCulture where possible.

    If you want the easy way out, you can do one of the following:

    Use an initializer

    Add one method to your test project and decorate it with the [AssemblyInitializer] attribute:

    [TestClass()]
    public sealed class CultureInitializer
    {
        [AssemblyInitialize()]
        public static void AssemblyInit(TestContext context)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
            Thread.CurrentThread.CurrentUiCulture = new CultureInfo("en-GB");
        }
    }
    

    Force the user profile culture settings

    I recommend you don't do this, while it shouldn't, it may influence other processes running after your tests, change output formats or cause other unwanted behaviours similar to what you're now experiencing.

    The data is stored in the registry in the following location:

    HKEY_CURRENT_USER\Control Panel\International
    

    You'll want to change the value before starting the tests, as these values are read from the system when starting the application unless you're registering for the events to be signalled when these values change, your tests will not see the difference.

    Run a script in the build definition before the tests start

    Set-Culture -CultureInfo en-GB