NUnit, testing against multiple cultures

五迷三道 提交于 2019-12-12 10:47:34

问题


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")]

回答1:


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?




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5991595/nunit-testing-against-multiple-cultures

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!