1

I have been searching Unit Test framework that is capable of testing asynchronous functional calls in Windows Phone 7 application development. Yes, Unit driven was the one pop up in my sight. However, that framework was not developer friendly as NUnit(which unfortunately cannot test asynch methods). On the internet, people has been using UD for Unit Testing. Could someone drop in and offer some advice?

My specific questions are:

1) Where do I include the following initialisation code?

private void Application_Startup(object sender, StartupEventArgs e)
{
      this.RootVisual = new UnitDriven.TestEngine(Assembly.GetExecutingAssembly());
}

2) How do I write test cases for UD? With NUnit, I can write test cases along with my application and NUnit loads my dll and execute tests. I have tried to put in my application but Visual Studio 2010 express always complain that it cannot find symbol GetContext()

UnitTestContext context = GetContext();

3) There were three dlls coming with UD. UnitDrivenLight, UnitDrivenPhone, UnitDrivenNet... So, what are the roles of UnitDrivenLight and UnitDrivenPhone? Very confusing at the moment.

Thanks

Simo

Simoscofield
  • 139
  • 2
  • 7

2 Answers2

1

I've not used UnitDriven myself, but I've had success with the testing framework that ships with the Windows Phone Toolkit (which supports asynchronous tests).

In fact, I've created a modified version which adds command line support. It's in NuGet as wp7-ci (the custom MSBuild task complicates manual installation).

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • which toolkit is it? In this page [link](http://silverlight.codeplex.com/), silverlight for windows phone toolkit Feb 2011 does not have test framework. I took the silverlight v3 from Jeff Wilcox's web site [link](http://www.jeff.wilcox.name/2010/05/sl3-utf-bits/). But in that version, I don't see [Asynchronous] tag as C#er wrote in this page [link](http://csharperimage.jeremylikness.com/2010/01/silverlight-unit-testing-framework.html) – Simoscofield Jul 18 '11 at 10:42
  • The [Asynchronous] tag certainly exists in wp7-ci, which is compiled from the same source. I don't use Jeff's version anymore as it doesn't support CI environments. – Richard Szalay Jul 18 '11 at 10:57
  • I obtained wp7-ci from NuGet. Still I don't see [Asynchronous] tag avaiable. How do you test asynchronous method using wp7-ci? Could you give me an example? – Simoscofield Jul 26 '11 at 13:32
  • After I included `using Microsoft.Silverlight.Testing;`, I got `[Asynchronous]` but still I would `Task` definition... `[TestMethod] [Asynchronous] public IEnumerable TestAsychronous()` { – Simoscofield Jul 26 '11 at 14:59
  • `Task` does not exist in Silverlight. I'm writing up a blog post on how to write asynchronous tests using `IObservable`, which will cover the topic of asynchronous tests. I'll post a link when I'm done. – Richard Szalay Jul 26 '11 at 15:07
  • I'm waiting for your blog post :) – Simoscofield Aug 01 '11 at 09:13
  • Well, due to the lack of information, I have to conclude now that wp7-ci does not support asynchronous testing at all. This is a dead end now. – Simoscofield Aug 08 '11 at 10:55
  • I've been on holidays and thus unable to work on my blog. Should be done in today or tomorrow. – Richard Szalay Aug 08 '11 at 12:14
  • @Simoscofield - I've created a blog post on asynchronous unit testing with the SL unit testing framework (including wp7-ci) - http://blog.richardszalay.com/2011/08/08/writing-asynchronous-unit-tests-with-rx-and-the-silverlight-unit-testing-framework/ – Richard Szalay Aug 08 '11 at 13:45
  • @Szalay, I found "EnqueueTestComplete();" could do the job. – Simoscofield Nov 03 '11 at 11:22
  • This works beautifully and executes asynchronous tests just fine. I followed this blog to make my tests asynchronous (http://jonas.follesoe.no/2008/06/04/unit-testing-asynchronous-silverlight-code/). Thanks for this Richard, it's a life saver! – Kam Sheffield Apr 05 '12 at 04:12
0

In the end, with Szalay's hints, I moved to use Microsoft's sliverlight test framework for asynchronous tests, here's the example test class:

namespace TestApp
{
    using System.Threading;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// An example test class
    /// </summary>
    [TestClass]
    public class ExampleTestClass : SilverlightTest
    {

        /// <summary>
        /// Sample asynchronous test
        /// </summary>
        [TestMethod, Tag("Asynchronous Test"), Asynchronous]
        public void SampleAsynchronousTest()
        {
            ThreadPool.QueueUserWorkItem(o =>
                {
                    for (int j = 0; j < 10000; j++){}
                    CheckResult(10);
                });
        }

        /// <summary>
        /// Check result
        /// </summary>
        /// <param name="variable">result</param>
        private void CheckResult(int variable)
        {
            Assert.IsTrue(variable == 10);
            EnqueueTestComplete();
        }
    }
}
Simoscofield
  • 139
  • 2
  • 7