C# How can I force Localization Culture to en-US for tests project

How to specify concrente Localization Culture for tests project in C# in VS2008? I'm building Asp .Net MVC app that has nonstandard culture specified in web.config but how to set the same culture for unit tests for that project?

Answers


You may set

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); 

in method that is marked as "test initializer" in your unit testing framework.


If you're using xUnit, you can add the UseCultureAttribute to your project, as defined here:

https://github.com/xunit/samples.xunit/blob/master/UseCulture/UseCultureAttribute.cs

To use it:

[Fact]
[UseCulture("en-US")]
public void MyTest()
{
    // ...
}

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;

For nUnit 3, you can use the attribute [SetUICulture("en-us")]. This will force the culture for this single test.


If you want to specify the CultureInfo for your entire Test Suite without having to add it in the TestInitializer of every TestClass, you can use the AssemblyInitializeAttribute.

Create a new TestClass, add a static method to it that sets DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture, and then decorate that method with AssemblyInitialzeAttribute. This method will then be run once when your test suite starts up, before any TestMethods are run. (Note: you may only have one such method decorated with this attribute in your test suite.)


There isn't a setting similar to the one in web.config that will work in your case.

You could try setting it for each thread as suggested by the other answers here.

Alternatively if you are using resources created in VS.NET, the code generation creates a static property on the Resource class called 'Culture'. You could set that in your unit test's Suite startup method. That will apply to all the tests that you run.


Need Your Help

Using jQuery Fancybox or Lightbox to display a contact form

jquery lightbox fancybox

I would like to use jQuery Fancybox or Lightbox to load a contact form from a standard link in a web page. I have reviewed the documents at http://fancybox.net/example but the closest option is the

WebView and localhost

android webview

I am getting google.com inside my WebView, but am not getting my localhost in that.