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")]
Chris Chilvers
  • 6,429
  • 3
  • 32
  • 53

4 Answers4

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?

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
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 :)

Patrick Stalph
  • 792
  • 1
  • 9
  • 19
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.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 1
    This project seams to be dead since at least half a year. – SOReader Feb 03 '14 at 12:03
  • 1
    @SOReader : What would you like it to do that it doesn't do now? – Mauricio Scheffer Feb 03 '14 at 13:28
  • 1
    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. – SOReader Feb 19 '14 at 22:39
  • 1
    @SOReader for support, there's stackoverflow, https://groups.google.com/forum/#!forum/mbunituser , https://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. – Mauricio Scheffer Feb 20 '14 at 03:06
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);
    }
}
Nicholas Mayne
  • 1,694
  • 14
  • 18