NUnit,针对多种文化进行测试

发布于 2024-11-07 01:10:26 字数 464 浏览 0 评论 0原文

我希望使用 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")]

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶底世界 2024-11-14 01:10:26

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

你也可以这样做。

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

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

也许这是你更喜欢的?

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?

゛清羽墨安 2024-11-14 01:10:26

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 将失败- 不同文化的测试非常重要:)

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

南烟 2024-11-14 01:10:26

如果您不介意切换,MbUnit 有这个该功能已使用近五年

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

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.

一刻暧昧 2024-11-14 01:10:26

如果有人正在寻找如何做到这一点 - 由于 NUnit 没有内置它,我这样做了......

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文