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?
8 Answers
You may set
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
in method that is marked as "test initializer" in your unit testing framework.
7 Comments
Thread.CurrentThread.CurrentUICulture and not CurrentCulture. The Intellisense for CurrentUICulture itself says Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time. My tests fail when using testing against an "fr-FR" culture resource using CurrentCulture, but pass when using CurrentUICulture.CultureInfo reference - I was questioning if it should be CurrentUICulture instead of CurrentCultureIf 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()
{
// ...
}
3 Comments
UseCultureAttribute you mean to copy the whole file into your project? Since it seems it is not included in the core nuget github.com/xunit/xunit/issues/290 and will never be.BeforeAfterTestAttribute which is part of xUnit.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.
Inside a Test Class (a class decorated with the attribute [TestClass]), add a static method that sets DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture, and then decorate that method with [AssemblyInitialize].
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.)
Here is an example of using a dedicated Test Class that just sets up the culture, but you can put it in any Test Class:
[TestClass]
public static class InitializeCulture
{
[AssemblyInitialize]
public static void SetEnglishCultureOnAllUnitTest(TestContext context)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
}
}
1 Comment
For nUnit 3, you can use the attributes [SetCulture("en-US")] and [SetUICulture("en-US")].
This will force the culture for this single test.
1 Comment
The following method worked for me:
[TestClass]
public class MyTestClass
{
[TestInitialize]
public void InitializeTestClass()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
}
.......... [other unit tests]
}
Comments
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.
Comments
I am using this approach to set the culture globally for a xUnit test project:
[assembly: TestFramework("My.Tests.AssemblyFixture", "My.Tests")]
namespace My.Tests;
public sealed class AssemblyFixture : XunitTestFramework {
public AssemblyFixture(IMessageSink messageSink) : base(messageSink) {
var culture = new CultureInfo("en-AU");
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
}
}
Credit to: How to set global setting for XUnit tests