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

后端 未结 6 1027
清酒与你
清酒与你 2021-02-02 07:33

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 s

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 07:42

    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");
        }
    }
    

提交回复
热议问题