NUnit, testing against multiple cultures
Asked Answered
A

4

10

Using NUnit I wish to run all the tests in a certain project against multiple cultures.

The project deals with parsing data that should be culture neutral, to ensure this I would like to run every test against multiple cultures.

The current solution I have is

public abstract class FooTests {
    /* tests go here */
}

[TestFixture, SetCulture ("en-GB")] public class FooTestsEN : FooTests {}
[TestFixture, SetCulture ("pl-PL")] public class FooTestsPL : FooTests {}

Ideally, I shouldn't have to create these classes and instead use something like:

[assembly: SetCulture ("en-GB")]
[assembly: SetCulture ("pl-PL")]
Aram answered 13/5, 2011 at 12:10 Comment(0)
E
7

Unfortunatelly this isn't possible now but is planned for future.

You can also do this.

public class AllCultureTests
{
  private TestSomething() {...}

  [Test]
  [SetCulture("pl-PL")]
  public void ShouldDoSomethingInPoland()
  {
    TestSomething();
  }
}

Maybe that's something you would prefer?

Eaglewood answered 14/5, 2011 at 19:45 Comment(1)
Looks like it is planned for NUnit 3 - bugs.launchpad.net/nunit-3.0/+bug/1093505Arella
V
5

NUnit's SetCultureAttribute applies one culture to a test, multiple cultures are not (yet) supported.

You can work around this by using the TestCaseAttribute with language codes and setting the culture manually:

    [Test]
    [TestCase("de-DE")]
    [TestCase("en-US")]
    [TestCase("da-DK")]
    public void YourTest(string cultureName)
    {
        var culture = CultureInfo.GetCultureInfo(cultureName);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        var date = new DateTime(2012, 10, 14);
        string sut = date.ToString("dd/MM/yyyy");
        Assert.That(sut, Is.EqualTo("14/10/2012"));
    }

Note that this unit test will fail for de and da - testing for different cultures is really important :)

Vitta answered 13/7, 2017 at 7:33 Comment(0)
H
1

If you don't mind switching, MbUnit has had this feature for nearly five years now.

You can apply the MultipleCulture attribute at the test, fixture and assembly levels.

Halliburton answered 20/9, 2011 at 22:13 Comment(4)
This project seams to be dead since at least half a year.Foothold
@Foothold : What would you like it to do that it doesn't do now?Halliburton
Like getting a support if I end up with not working functionalities? It appears risky to me learning new library which has become dead recently.Foothold
@Foothold for support, there's stackoverflow, groups.google.com/forum/#!forum/mbunituser , groups.google.com/forum/#!forum/gallio-user . And the source code. There's hardly anything to learn about MbUnit if you already know NUnit, it just has more features.Halliburton
C
0

If anyone is looking for how to do this - as NUnit doesnt have it built in, I did this...

[SetUpFixture]
public class TestFixtureProjectSetup
{
    [OneTimeSetUp]
    public void RunBeforeAllTestFixtures()
    {
        var cultureCode = Environment.GetEnvironmentVariable("NUNIT_TEST_CULTURE");

        if (!String.IsNullOrWhiteSpace(cultureCode))
        {
            TestExecutionContext.CurrentContext.CurrentCulture = CultureInfo.GetCultureInfo(cultureCode);
        }

        TestContext.Progress.WriteLine("Running Tests using CULTURE:{0}", cultureCode);
    }
}
Crispate answered 29/7, 2023 at 23:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.