Creating unit tests

Hi all I am trying to creat unit tests for a progam i priviously created, my Calculator program.

How in hadies do I do this??

Here is the code for my program.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SuperCalculator
{
    public partial class pnlclear : Form
    {
        public pnlclear()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }
        #region Methods
        public void Add()
        {
            checker();
            decimal sum = Convert.ToDecimal(tbfirstnumber.Text) +
             Convert.ToDecimal(tbsecondnumber.Text);
            tbanswer.Text = sum.ToString();
        }

        public void Subtract()
        {
            checker();
            decimal sum = Convert.ToDecimal(tbfirstnumber.Text) - Convert.ToDecimal(tbsecondnumber.Text);
            tbanswer.Text = sum.ToString();
        }

        public void Multiply()
        {
            checker();
            decimal sum = Convert.ToDecimal(tbfirstnumber.Text) * Convert.ToDecimal(tbsecondnumber.Text);
            tbanswer.Text = sum.ToString();
        }

        public void Divide()
        {
            checker();
            decimal sum = Convert.ToDecimal(tbfirstnumber.Text) / Convert.ToDecimal(tbsecondnumber.Text);
            tbanswer.Text = sum.ToString();
        }


        public void SquareRoot()
        {
            checker();
            double num = Convert.ToDouble(tbfirstnumber.Text);
            if
                (num > 0)
            {
                double sqrt = Math.Sqrt(num);
                tbanswer.Text = sqrt.ToString();
            }
            else

            { tbanswer.Text = "Square root can only be taken from positive number."; }
        }

        public void Percentage()
        {
            checker();
            decimal pct = Convert.ToDecimal(tbfirstnumber.Text) * 100m / Convert.ToDecimal(tbsecondnumber.Text);
            tbanswer.Text = pct.ToString("0.00");
        }
        public void Power()
        {
            checker();
            decimal pow = (decimal)Math.Pow(Convert.ToDouble(tbfirstnumber.Text),
             Convert.ToDouble(tbsecondnumber.Text));
            tbanswer.Text = pow.ToString();
        }

        public void Ratio()
        {
            checker();
            // private void button1_Click(object sender, EventArgs e)  
            {
                int n1 = Convert.ToInt32(tbfirstnumber.Text);
                int n2 = Convert.ToInt32(tbsecondnumber.Text);
                //int n1 = 75;
                // int n2 = 100;
                tbanswer.Text = (n1 / Euclidean(n1, n2) + ":" + n2 / Euclidean(n1, n2));
                // tbanswer.Text = n3.ToString();
            }
        }
        public static int Euclidean(int aa, int bb) { int d = 0; while (bb != 0) { d = bb; bb = aa % bb; aa = d; } return d; }

        public void KeepGoing()
        {
            tbfirstnumber.Text = tbanswer.Text;
            tbsecondnumber.Clear();
            tbanswer.Clear();
        }

        public void Clear()
        {
            tbanswer.Clear();
            tbfirstnumber.Clear();
            tbsecondnumber.Clear();
        }

        public void checker()
        {
            //if (txtUserName.Text.Trim().Length <= 0 && txtPassword.Text.Trim().Length <= 0)
            //{
            //    MessageBox.Show("Please enter User Name and Password.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    this.myResult = DialogResult.Cancel;
            //    return;
            //}
            if (tbfirstnumber.Text.Trim().Length <= 0 && tbsecondnumber.Text.Trim().Length <= 0)
            {
                MessageBox.Show("There must be a number in the question boxes.");
                this.DialogResult = DialogResult.Cancel;
                this.Clear();
                tbfirstnumber.Focus();
            }

        }
        #endregion Methods

        #region ButtonClicks


        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add();

            //pnlAdd2.Visible = true;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            Subtract();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = true;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            Multiply();

            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = true;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            Divide();

            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = true;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnSqrt_Click(object sender, EventArgs e)
        {
            SquareRoot();

            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = true;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Percentage();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = true;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnEnumeratior_Click(object sender, EventArgs e)
        {
            Ratio();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = true;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnPowers_Click(object sender, EventArgs e)
        {
            Power();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = true;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = false;
        }

        private void btnKeepGoing_Click(object sender, EventArgs e)
        {
            KeepGoing();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = true;
            //pnlClearFields.Visible = false;
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
            //pnlAdd2.Visible = false;
            //pnlsub.Visible = false;
            //pnlMultiply.Visible = false;
            //pnlDivide.Visible = false;
            //pnlSquareRoot.Visible = false;
            //pnlPercentage.Visible = false;
            //pnlRatio.Visible = false;
            //pnlPowers.Visible = false;
            //pnlKeepGoing.Visible = false;
            //pnlClearFields.Visible = true;
        }

        #endregion Button clicks










    }
}
HawaiiDragonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

craigdevCommented:
Test driven development is about having a thin UI layer on top of rigourously tested and well structured code. If you want to apply test driven development I would recommend learning the Model- View-Presender design pattern. You can unit-test your controllers by making them implement the 'view' functions and essentially swap out your UI layer for a unit test project.

In MVP your form doesn't do anything other than render data and respond to user events like button clicks. The form implements a 'view' interface and  you have a separate controller class that does all the work. Your controller can see the form through the view and the view calls a function on the controller. THe view and controller together are called an interaction point.

A good place to start is here www.mvcsharp.org and here www.nunit.org or - my personal preference - www.xunit.org
0
craigdevCommented:
==== example ====

Imports xUnit

Namespace SuperCalculator

    Public Class mathController

        Private mScreen As iPanelView

        Public Sub New(ByVal screen As iPanelView)
            Me.mScreen = screen
        End Sub

        Public Sub Add(ByVal firstNumber As Decimal, ByVal secondNumber As Decimal)
            Dim sum As Decimal = firstNumber + secondNumber
            mScreen.SetValue(sum)
        End Sub

        Public Sub Subtract(ByVal firstNumber As Decimal, ByVal secondNumber As Decimal)
            Dim sum As Decimal = firstNumber - secondNumber
            mScreen.SetValue(sum)
        End Sub

        Public Sub Multiply(ByVal firstNumber As Decimal, ByVal secondNumber As Decimal)
            Dim sum As Decimal = firstNumber * secondNumber
            mScreen.SetValue(sum)
        End Sub

        Public Sub Divide(ByVal firstNumber As Decimal, ByVal secondNumber As Decimal)
            Dim sum As Decimal = firstNumber / secondNumber
            mScreen.SetValue(sum)
        End Sub

    End Class

    Public Interface iPanelView
        Sub SetValue(ByVal answer As Decimal)
    End Interface

    Partial Public Class pnlclear
        Inherits Form
        Implements iPanelView

        Private mController As mathController

        Public Sub New()
            InitializeComponent()
            mController = New mathController(Me)
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
            mController.Add(Me.tbFirstNumber.Text, Me.tbSecondNumber.Text)
        End Sub

        Private Sub btnSubtract_Click(ByVal sender As Object, ByVal e As EventArgs)
            mController.Subtract(Me.tbFirstNumber.Text, Me.tbSecondNumber.Text)
        End Sub


        Private Sub btnMultiply_Click(ByVal sender As Object, ByVal e As EventArgs)
            mController.Multiply(Me.tbFirstNumber.Text, Me.tbSecondNumber.Text)
        End Sub

        Private Sub btnDivide_Click(ByVal sender As Object, ByVal e As EventArgs)
            mController.Divide(Me.tbFirstNumber.Text, Me.tbSecondNumber.Text)
        End Sub
     Public Sub SetValue(ByVal answer As Decimal) Implements iPanelView.SetValue
            mAnswer = answer
        End Sub

     Public Sub SetValue(ByVal answer As Decimal) Implements iPanelView.SetValue
            tbAnswerTextbox.Text = answer.ToString
        End Sub

    End Class

    'THis is the unit test, where you can assert that the expected answer was what you expected, or not
    Public Class MathTestFixture
        Implements iPanelView

        Private mAnswer As Decimal
        Private mController As mathController

        Public Sub New()
            mController = New mathController(Me)
        End Sub

        [Fact]
        Public Sub TestAddittion()
            mAnswer = 0
            mController.Add(1, 2)
            Assert.True(mAnswer = 3)
        End Sub

        Public Sub SetValue(ByVal answer As Decimal) Implements iPanelView.SetValue
            mAnswer = answer
        End Sub

    End Class

End Namespace
0
HawaiiDragonAuthor Commented:
okay but any of these examples in C# not VB?
0
craigdevCommented:
its just one example using mvc and unit testing your addition function

http://www.developerfusion.com/tools/convert/vb-to-csharp/


using xUnit;


namespace SuperCalculator
{
   
    public class mathController
    {
       
        private iPanelView mScreen;
       
        public mathController(iPanelView screen)
        {
            this.mScreen = screen;
        }
       
        public void Add(decimal firstNumber, decimal secondNumber)
        {
            decimal sum = firstNumber + secondNumber;
            mScreen.SetValue(sum);
        }
       
        public void Subtract(decimal firstNumber, decimal secondNumber)
        {
            decimal sum = firstNumber - secondNumber;
            mScreen.SetValue(sum);
        }
       
        public void Multiply(decimal firstNumber, decimal secondNumber)
        {
            decimal sum = firstNumber * secondNumber;
            mScreen.SetValue(sum);
        }
       
        public void Divide(decimal firstNumber, decimal secondNumber)
        {
            decimal sum = firstNumber / secondNumber;
            mScreen.SetValue(sum);
        }
    }
   
   
    public interface iPanelView
    {
        void SetValue(decimal answer);
    }
   
    public partial class pnlclear : Form, iPanelView
    {
       
        private mathController mController;
       
        public pnlclear()
        {
            InitializeComponent();
            mController = new mathController(this);
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
       
        private void btnAdd_Click(object sender, EventArgs e)
        {
            mController.Add(this.tbFirstNumber.Text, this.tbSecondNumber.Text);
        }
       
        private void btnSubtract_Click(object sender, EventArgs e)
        {
            mController.Subtract(this.tbFirstNumber.Text, this.tbSecondNumber.Text);
        }
       
       
        private void btnMultiply_Click(object sender, EventArgs e)
        {
            mController.Multiply(this.tbFirstNumber.Text, this.tbSecondNumber.Text);
        }
       
        private void btnDivide_Click(object sender, EventArgs e)
        {
            mController.Divide(this.tbFirstNumber.Text, this.tbSecondNumber.Text);
        }
           
        public void SetValue(decimal answer)
        {
            tbAnswerTextbox.Text = answer.ToString;
        }
    }
   
   
    //THis is the unit test, where you can assert that the expected answer was what you expected, or not
    public class MathTestFixture : iPanelView
    {
       
        private decimal mAnswer;
        private mathController mController;
       
        public MathTestFixture()
        {
            mController = new mathController(this);
        }
       
        var Fact;
        public void TestAddittion()
        {
            mAnswer = 0;
            mController.Add(1, 2);
            Assert.True(mAnswer == 3);
        }
       
        public void SetValue(decimal answer)
        {
            mAnswer = answer;
        }
    }
   
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.