如何在 MSTest 中模拟 MbUnit 的 [MultipleCulture] 属性?

发布于 2024-08-01 14:24:05 字数 363 浏览 3 评论 0原文

MbUnit 有一个很棒的属性:MultipleCultureAttribute

我想知道在 MSTest 中是否有一种简单的方法可以做到这一点? 到目前为止,我能想到的最好办法是:

  • 将实际测试代码外部化为私有方法
  • 保存当前区域性
  • 设置区域性并调用私有方法(对每种区域性重复)
  • 最后,恢复到原始区域性

最多,可以用丑陋……和冗长来形容。

MbUnit has a great attribute: MultipleCultureAttribute.

I am wondering if there is an easy way to do this in MSTest? So far, the best I can come up with is:

  • Externalizating the actual test code to a private method
  • Saving the current culture
  • Setting the culture and calling the private method (repeated for each culture)
  • And finally, reverting to the original culture

At best, it can be described as ugly ... and verbose.

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

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

发布评论

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

评论(1

少跟Wǒ拽 2024-08-08 14:24:05

最简单的方法可能是使用匿名委托,但请记住,MSTest 会将其视为单个测试,因此可能很难区分不同文化的结果。

例如。 匿名委托方法的粗略代码。

public static class MultipleCultures
{
    public static void Do(Action action, params CultureInfo[] cultures)
    {
        CultureInfo originalCulture = Thread.CurrentCulture;

        try
        {
            foreach (CultureInfo culture in cultures)
            {
                Thread.CurrentCulture = culture;

                try
                {
                    action();
                }
                catch
                {
                    Console.WriteLine("Failed while running test with culture '{0}'.", culture.Name);
                    throw;
                }
            }
        }
        finally
        {
            Thread.CurrentCulture = originalCulture;
        }
    }
}

[TestClass]
public class Fixture
{
    [TestMethod]
    public void Test()
    {
        MultipleCultures.Do(() =>
        {
            // test code...
        }, CultureInfo.InvariantCulture, CultureInfo.GetCulture("en-GB"));
    }
}

The simplest approach may be to use an anonymous delegate, however keep in mind that MSTest will treat this as a single test so it may be difficult to distinguish results for different cultures.

eg. Rough code for anonymous delegate approach.

public static class MultipleCultures
{
    public static void Do(Action action, params CultureInfo[] cultures)
    {
        CultureInfo originalCulture = Thread.CurrentCulture;

        try
        {
            foreach (CultureInfo culture in cultures)
            {
                Thread.CurrentCulture = culture;

                try
                {
                    action();
                }
                catch
                {
                    Console.WriteLine("Failed while running test with culture '{0}'.", culture.Name);
                    throw;
                }
            }
        }
        finally
        {
            Thread.CurrentCulture = originalCulture;
        }
    }
}

[TestClass]
public class Fixture
{
    [TestMethod]
    public void Test()
    {
        MultipleCultures.Do(() =>
        {
            // test code...
        }, CultureInfo.InvariantCulture, CultureInfo.GetCulture("en-GB"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文