【问题标题】:Asynchronous tests in VSUTF, NUnit, xUnit.NET, MbUnit vs. SUTF?VSUTF、NUnit、xUnit.NET、MbUnit 与 SUTF 中的异步测试?
【发布时间】:2012-01-31 06:15:33
【问题描述】:

Silverlight 单元测试框架有一个 [Asynchronous] 属性 (AsynchronousAttribute),它会导致测试仅在 EnqueueTestComplete() 被调用时结束。这允许一种简单的方法来编写需要在事件结束之前等待事件发生的测试。现在我试图从似乎是最流行的选择中挑选一个最喜欢的通用单元测试框架——VSUTF、NUnit、xUnit.NET、MbUnit,我想知道——你将如何使用这些框架进行异步测试?

我想我可以推出一些自定义代码来执行 Monitor.Wait 或 ResetEventWaitOne 并在测试方法结束时调用它,然后在测试结束时执行 Pulse/Set,但我正在寻找是否存在是现有的通用/内置解决方案。

这是如何在 SUT 中完成的示例(来自 http://smartypantscoding.com/a-cheat-sheet-for-unit-testing-silverlight-apps-on-windows-phone-7)。

[TestClass]
public class AsyncTests : SilverlightTest
{
    [Asynchronous]
    [TestMethod]
    public void AsyncAppendStringTest()
    {
        var appendStrings = new List<string>() { "hello", "there" };

        StringJoiner.AsyncAppendStringsWithDashes(appendStrings, (returnString) =>
            {
                Assert.IsTrue(string.Compare(returnString, "hello-there") == 0);
                EnqueueTestComplete();
            });
    }
}

【问题讨论】:

    标签: c# nunit mstest mbunit xunit.net


    【解决方案1】:

    受 Caliburn Micro 启发,Christopher Bennage 对此提出了一种有趣的方法:

    http://devlicio.us/blogs/christopher_bennage/archive/2011/01/17/improving-asynchronous-tests-for-silverlight.aspx

    【讨论】:

    • 这仍然是基于 Silverlight 单元测试的解决方案,尽管从我所见。其他非 Silverlight 框架呢?
    【解决方案2】:

    Visual Studio 现在支持具有async Task 签名的测试,并且测试在异步方法完成时完成。

    【讨论】:

      猜你喜欢
      • 2010-10-13
      • 2011-04-10
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多