Translate

Thursday, 4 October 2012

A sneak preview - Sample NUnit async test

Since NUnit is being made aware of C# 5 new features, here is a sample unit test on async and await.

The current NUnit downlaod may not work so wait for some time before a stable build is announced.


using NUnit.Framework;
using System.Threading.Tasks;
using System.Threading;
namespace Net45.UnitTests.Samples.Jv
{
    [TestFixture]
    public class AsyncTest
    {
        int x = 0;
        [Test]
        public async void testAddition(){                    
            Assert.AreEqual(15, await returnAdditionResult());
        }
        [Test]
        public async void testAdd()
        {
            Assert.AreEqual(17, await add(9,8));
        }
        private async Task add(int a, int b)
        {
            return a + b;
        }
        private async Task returnAdditionResult()
        {
           return await add(9, 7);        
        }
    }
}

No comments: