Forum Xamarin.Android

Unit Testing and Async / Await

CameronVetter.6636CameronVetter.6636 USMember, University

I'm new to Xamarin and have been using Xamarin studio for about a week, and have been struggling to get the Unit testing to function properly with async calls. I'm hoping I just don't understand how to use it properly and it actually works right with async calls, but I'm starting to wonder. If I have a test method:

        async public void Create ()
            var charactersObject = new Characters ("unitTestToken");
            Characters.Character expected = new Characters.Character ();

            expected.userId = "123456";
   = "TestUser";

            var actual = await charactersObject.Create (expected);

            Assert.AreEqual (expected.userId, actual.userId, "User Id are not equal");
            Assert.AreEqual (,, "Name are not equal");
            Assert.AreNotEqual (0,;

and have the following code:

        async public Task Create (Character newCharacter)
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(newCharacter.toJSON());

            string url = BASE_URL + VERSION + "Characters";
            var request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add ("authorization", authorization_token);
            request.ContentLength = byteArray.Length;

            Stream writer = await request.GetRequestStreamAsync ();
            writer.Write (byteArray, 0, byteArray.Length);
            await writer.FlushAsync ();
            writer.Close ();

            WebResponse ar = await request.GetResponseAsync ();
            var s = new StreamReader(ar.GetResponseStream ());

            string json = await s.ReadToEndAsync ();

            return new Character (json);

I can clearly see that the test method completes and reports that it passes before it ever even hits the asserts. In fact if an assertion ends up failing, I see the test method passing in the test runner and I get an exception for the failed assertion. It seems that my Create method is still finishing up but passes control back to the unit test on one of the long running async calls.

I've verified that the code does actually work correctly outside of the unit test runner. I've also tried googling and searching the Xamarin docs, but it seems that most information is about the IOS unit testing framework which is more advanced then the android one.

Thanks for any help anyone has to offer.


  • CameronVetter.6636CameronVetter.6636 USMember, University

    I can't believe no one else out there has tried to unit test with the nunitlite package that comes wit Xamarin Android... Is there a better version or unit testing package out there for Xamarin Android that I should be using? I've had success with every other aspect of Xamarin Android, but the lack of functioning unit testing is a killer. I've got 9 days left on my trial and this is the one thing keeping me from making the purchase.

  • Hey @CameronVetter,

    I'm actually having the exact same problem as we speak. I've investigated this quite a bit and my current suspicion are bugs in the NUnitLite version that ships with Xamarin.

    I posted earlier today asking what are Xamarin's plans. But I haven't heard anything yet.

    If I find some time, I'd like to try NUnitLite 1.0 to see if it fixes the problems with async tests.

    Have you tried running this test on Xamarin.iOS?

  • CameronVetter.6636CameronVetter.6636 USMember, University
    edited February 2014

    No I haven't tried Xamarin.IOS yet, I was trying to dig in deep into one platform before I bought. I also suspect it is bugs in the the NUnitLite version currently shipping with Xamarin, but haven't dug into the possibility of integrating a newer version. I looked through the release notes of everything after .9 and it didn't sound like anything was fixed relating to this so hadn't tried it yet.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    I know, a lot late, but a bug report has been filed for this issue, which may very well be an upstream bug as we do not maintain NUnitLite, just the runner.

    Link to bug report:

  • KrishnaPatelKrishnaPatel INMember

    Is there any workaround for this? I want to write some integration tests which involves making async call to the server - this bug is a show-stopper.

  • softlionsoftlion FRBeta ✭✭✭

    Visual Studio unit tests have the same bugs related to async tests. So it may be an issue with C#.

    I'm unit testing using Ms Framework my PCL libraries, and to test async code i simply call it synchonously, by adding .Result after each task instead of awaiting it. Also i don't have the async keyword on the test method.

    This workaround works perfectly fine. You may try it.

Sign In or Register to comment.