Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit, testing against multiple cultures

Tags:

nunit

culture

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")]
like image 263
Chris Chilvers Avatar asked May 13 '11 12:05

Chris Chilvers


People also ask

How do you write multiple test cases in NUnit?

Make more copies of the attribute if you want multiple cases. The data type of the values provided to the TestCase attribute should match with that of the arguments used in the actual test case.

What type of testing does NUnit support?

NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.

Can we use MOQ with NUnit?

We will install NUnit and Moq using the Nuget package manager. Make sure that in your references, NUnit and Moq are present after installation: For running NUnit tests and exploring the tests, we need to install a visual studio extension called “NUnit 3 Test Adapter”.

Can we write test cases for private methods in NUnit?

Unit Tests Should Only Test Public Methods The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.


2 Answers

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?

like image 187
Piotr Perak Avatar answered Oct 26 '22 08:10

Piotr Perak


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 :)

like image 34
Patrick Stalph Avatar answered Oct 26 '22 10:10

Patrick Stalph