# 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
{
checker();
decimal sum = Convert.ToDecimal(tbfirstnumber.Text) +
Convert.ToDecimal(tbsecondnumber.Text);
}

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

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

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

public void SquareRoot()
{
checker();
double num = Convert.ToDouble(tbfirstnumber.Text);
if
(num > 0)
{
double sqrt = Math.Sqrt(num);
}
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);
}
public void Power()
{
checker();
decimal pow = (decimal)Math.Pow(Convert.ToDouble(tbfirstnumber.Text),
Convert.ToDouble(tbsecondnumber.Text));
}

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));
}
}
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()
{
tbsecondnumber.Clear();
}

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

public void checker()
{
//{
//    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)
{

//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();
//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();

//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();

//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();

//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();
//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();
//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();
//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();
//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();
//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

}
}
###### Who is Participating?

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.

Commented:
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
Commented:
==== 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
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)
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
End Sub

Public Sub SetValue(ByVal answer As Decimal) Implements iPanelView.SetValue
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 mController As mathController

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

[Fact]
End Sub

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

End Class

End Namespace
0
Author Commented:
okay but any of these examples in C# not VB?
0
Commented:
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
{
}

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)
{
}

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);
}

{
}
}

//THis is the unit test, where you can assert that the expected answer was what you expected, or not
public class MathTestFixture : iPanelView
{

private mathController mController;

public MathTestFixture()
{
mController = new mathController(this);
}

var Fact;
{
}

{
}
}

}

0

Experts Exchange Solution brought to you by