[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating unit tests

Posted on 2008-11-03
4
Medium Priority
?
456 Views
Last Modified: 2013-12-17
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










    }
}
0
Comment
Question by:HawaiiDragon
  • 3
4 Comments
 
LVL 1

Expert Comment

by:craigdev
ID: 22871965
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
 
LVL 1

Expert Comment

by:craigdev
ID: 22872126
==== 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
 

Author Comment

by:HawaiiDragon
ID: 22876323
okay but any of these examples in C# not VB?
0
 
LVL 1

Accepted Solution

by:
craigdev earned 2000 total points
ID: 22879800
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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question