Wrong CurrentCulture when running an nUnit test in TeamCity
Asked Answered
G

5

6

I have a unit-test that relies on a specific culture.

In FixtureSetup, I set both Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture to the desired value (en-US).

When I run the test from Resharper, it passes.

When I run the test from TeamCity (using the runner "NUnit 2.4.6"), the test fails, because CurrentCulture is cs-CZ (the culture of my OS). However CurrentUICulture is still en-US.

Gearalt answered 19/1, 2011 at 7:35 Comment(0)
S
11

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;
  }
}
Spirituous answered 19/1, 2011 at 8:26 Comment(4)
This is exactly what I have done. It works when running my unit-tests in Resharper, it doesn't when I run them in TeamCity.Parental
How do you run nUnit in TeamCity ?Spirituous
Have you tried using the [SetCulture("en-US")] attribute ? http://www.nunit.org/index.php?p=setCulture&r=2.5.9Spirituous
have you tried to define your AssemblyCulture in the AssemblyInfo.cs? [assembly: AssemblyCulture("en-US")]Novelize
G
3

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.

Gearalt answered 15/2, 2011 at 7:44 Comment(0)
I
3

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

Identify answered 9/12, 2016 at 12:13 Comment(0)
E
1

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;
            }
Ensign answered 18/4, 2013 at 11:38 Comment(2)
Are you sure you need to restore the culture? I tried quickly and it seems to me that the NUnit framework restores the culture so it doesn't leak into other tests.Spare
May be NUnit does better isolation between different tests. I had a problem with Microsoft Tests when running VSTest.Console.Ensign
D
0

I have a similar problem. TeamCity somehow ignores CultureInfo instances I pass on the fly. Same tests and methods have been working as expected on all other platforms and runners (resharper, mstest, ncrunch and many more.) for almost 15 years. My case is not about managing culture contexts (UICulture, Thread, SetCulture etc.). It must be messing with .NET framework configurations or something. Confusing.

[Test]
public void WhatIsGoingOnWithTheCulture()
{
     //This method should pass. It should return "İ", it is Turkish letter.
     Assert.AreEqual("İ","i".ToUpper(new CultureInfo("tr-TR")));
}

//String lengths are both 1. Strings differ at index 0.
//  Expected: "Ý"
//  But was:  "İ"

TeamCity: 2020.2 (build 85487), tests with Nunit3 and NUnitConsole 3.11.1

.Net Framework 4.8

Dnepropetrovsk answered 5/12, 2020 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.