Wednesday 15 October 2014

Unit Tests

A unit test, is an automated way to test your code, it takes some getting use to but well worth the effort. for now let's go over how to create a unit test project, a simple test and getting it to work.

don't bother with this go to

https://msdn.microsoft.com/library/hh440545(v=vs.110)#CreateAndRunUnitTestWin8Tailored_Manifest

i only made this post because it cements the procedure in my head.

Let's create a very simple class that takes in two number and adds them together. 

first our award winning UI
<Page
    x:Class="UnitTestExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UnitTestExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel VerticalAlignment="Center" Margin="100">
            <TextBox Header="First Number:" x:Name="FirstNumber_TXT" />
            <TextBox Header="Second Number:" x:Name="SecondNumber_TXT" />
            <TextBox Header="Answer" x:Name="Answer_TXT" />
            <Button Content="Add" x:Name="Add_BTN" />
        </StackPanel>
    </Grid>
</Page>


with that done let's take a look at our codebehind,

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace UnitTestExample
{
    public class OurMath {

        public string Add(int A, int B)
        {
            return (A + B).ToString();
        }
    }

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Add_BTN.Click += Add_BTN_Click;
        }

        void Add_BTN_Click(object sender, RoutedEventArgs e)
        {
            var a = Convert.ToInt32(this.FirstNumber_TXT.Text);
            var b = Convert.ToInt32(this.SecondNumber_TXT.Text);

            this.Answer_TXT.Text = new OurMath().Add(a,b);
        }
    }
}


Simple enough, now let's test our function, first add a new project and specify a 

with done we should have the following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

namespace OurTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

Now before we can actually write any test, we have to make reference to our main project from our test project,

First in your solution explorer right click on the references item and click add reference.


here let's pick our project and hit OK

Now we're ready to rumble, lets rename our test class to OurMathTests and lets rename our test class to TestOurAdd, and add our test statement.

using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using UnitTestExample;

namespace OurTests
{
    [TestClass]
    public class OurMathTests
    {
        [TestMethod]
        public void TestAddMethod()
        {
            var ourMath = new OurMath();

            Assert.AreEqual(ourMath.Add(5, 4), "9");
        }
    }
}


and that's it, run your test's and all should be well, one thing to remember is that the unit test project also has an app manifest with capabilities that have to be appropriately configured.