Last week after introducing localization into our application our builds started to fail. Inside the build log we saw that only tests related to localization were failing.
What happened? The problem was that the tests where expecting a specific culture to be set which was different on our local machines compared to the build server. So we needed a way to set the (UI)culture for our tests.
A solution that worked for us was to set the CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture in the Assembly initializer of our tests:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class CultureInitializer | |
{ | |
[AssemblyInitialize] | |
public static void AssemblyInit(TestContext context) | |
{ | |
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-BE"); | |
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-BE"); | |
} | |
} |
Remark: Don’t forget to add the [TestClass] attribute on top of your class otherwise MSTest will not invoke the AssemblyInitialize method.