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

1

7

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?

Koon answered 3/2, 2018 at 12:28 Comment(0)
E
6

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
Exteroceptor answered 3/2, 2018 at 13:46 Comment(4)
thanks jessehouwing for your answer, I would prefer the 2nd suggestion, as the first one would be too many initialisations for numbers of test projects/classes. But, as I am using VSTS online, I guess I can't do something like that? ThanksKoon
Powershell can change the regional setting by overwriting the existing values for the build agent user. But please, fix your tests.Exteroceptor
Yes, as you enlightened, I've added a powershell script to change the culture before running unit tests, now it is working. I should have fix the tests, but that is for later, Thanks a lot.Koon
It would be helpful to share the script. Feel free to edit my answer and just add it.Exteroceptor

© 2022 - 2024 — McMap. All rights reserved.