【问题标题】:NUnit, testing against multiple culturesNUnit,针对多种文化进行测试
【发布时间】:2011-05-13 12:10:05
【问题描述】:

使用 NUnit 我希望在某个项目中针对多种文化运行所有测试。

该项目处理应该是文化中立的解析数据,以确保这一点,我希望针对多种文化运行每个测试。

我目前的解决方案是

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

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

理想情况下,我不应该创建这些类,而是使用类似的东西:

[assembly: SetCulture ("en-GB")]
[assembly: SetCulture ("pl-PL")]

【问题讨论】:

    标签: nunit culture


    【解决方案1】:

    很遗憾,现在这是不可能的,但计划在未来实现。

    你也可以这样做。

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

    也许这是你更喜欢的东西?

    【讨论】:

    【解决方案2】:

    NUnit 的SetCultureAttribute一种 文化应用于测试,但(尚)不支持多种文化。

    您可以通过使用带有语言代码的TestCaseAttribute 并手动设置文化来解决此问题:

        [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"));
        }
    

    请注意,对于 deda,此单元测试将失败 - 测试不同文化非常重要 :)

    【讨论】:

      【解决方案3】:

      如果你不介意切换,MbUnit 有这个feature for nearly five years now

      您可以在测试、夹具和装配级别应用MultipleCulture 属性。

      【讨论】:

      • 这个项目至少半年就死了。
      • @SOReader : 你希望它做什么而不是现在?
      • 如果我最终无法使用功能,想要获得支持吗?学习最近已经死掉的新图书馆对我来说似乎很冒险。
      • @SOReader 支持,有 stackoverflow,groups.google.com/forum/#!forum/mbunitusergroups.google.com/forum/#!forum/gallio-user。和源代码。如果您已经了解 NUnit,几乎不需要了解 MbUnit,它只是具有更多功能。
      猜你喜欢
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多