Pregunta

Tengo una unidad de prueba que se basa en una cultura específica.

En FixtureSetup, I establece tanto Thread.CurrentThread.CurrentCulture y Thread.CurrentThread.CurrentUICulture al valor deseado (en-US).

Cuando funciono la prueba de ReSharper, pasa.

Al ejecutar la prueba de TeamCity (utilizando el corredor "NUnit 2.4.6"), la prueba falla, porque CurrentCulture es cs-CZ (la cultura de mi sistema operativo). Sin embargo sigue siendo CurrentUICulture en-US.

¿Fue útil?

Solución

You can force a specific culture for running your tests in your current thread System.Threading.Thread.CurrentThread

// set CurrentCulture to Invariant
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
// set UI culture to invariant
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

You can also use CultureInfo.GetCultureInfo to provide the culture you want to use. This can be down in the SetUp part of your tests.

Remember to restore the culture to the previous one in your TearDown to ensure isolation

[TestFixture]
class MyTest {
  CultureInfo savedCulture;

  [SetUp]
  public void SetUp() {
    savedCulture = Thread.CurrentThread.CurrentCulture;
    Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  }

  [TearDown]
  public void TearDown() {
    Thread.CurrentThread.CurrentCulture = savedCulture;
  }
}

Otros consejos

It seems like TeamCity is running FixtureSetup and unit-test in different threads, or somehow modifying CurrentUICulture.

Setting both CurrentUICulture and CurrentCulture in SetUp (instead of FixtureSetup) solved the problem.

Starting with NUnit 2.4.2, you can use the SetCulture Attribute.

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  [SetCulture("fr-FR")]
  public class FrenchCultureTests
  {
    // ...
  }
}

The example is taken from the link below. Please also refer to the link for further details.

https://github.com/nunit/docs/wiki/SetCulture-Attribute

In my test I've set and reset CurrentUICulture within individual test method

      

            var tempCurrentUICulture = Thread.CurrentThread.CurrentUICulture;
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-HK" );
                 actual = target.MethodToTest(resourceSet, localeId);
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = tempCurrentUICulture;
            }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top