如何为Windows窗体应用程序编写NUnit测试用例?

时间:2017-03-28 09:34:54

标签: winforms nunit

以下是我的计算器程序。如何编写NUnit测试用例进行添加 功能?

我想为CalculatorTest创建一个单独的项目,我希望通过为Form1创建一个对象来访问Form1。

这里我无法在CalculatorTest中为Form1创建一个对象。

注意:我添加了nunit和我的计算器命名空间的引用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Calculater
{
public partial class Form1 : Form
{
    double value;
    string operator_clicked;
    bool isResult = false;

    public Form1()
    {
        InitializeComponent();
        txtresult.Text = "0";
    }

    private void btn1_Click(object sender, EventArgs e)
    {
        if (txtresult.Text == "0" || isResult)
        { 
            txtresult.Clear();
            isResult = false;
        }
        Button btn = (Button)sender;
        txtresult.Text = txtresult.Text + btn.Text;
    }

    private void btnclr_Click(object sender, EventArgs e)
    {
        txtresult.Text="0";
        value = 0;
    }


    private void btnequal_Click(object sender, EventArgs e)
    {
        if(value>=0 && txtresult.Text!=string.Empty)
        { 
                switch(operator_clicked)
                {
                    case "+":
                        txtresult.Text = (value + double.Parse(txtresult.Text)).ToString();
                        break;
                    case "-":
                        txtresult.Text = (value - double.Parse(txtresult.Text)).ToString();
                        break;
                    case "X":
                        txtresult.Text = (value * double.Parse(txtresult.Text)).ToString();
                        break;
                    case "/":
                        try
                        {

                            txtresult.Text = (value / double.Parse(txtresult.Text)).ToString();
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            txtresult.Clear();
                        }
                        break;
                }
                txttop.Clear();
                isResult = true;
        }
    }

    private void btnOperator_Click(object sender, EventArgs e)
    {
        if(txtresult.Text!=string.Empty)
        {
        Button btnOperator = (Button)sender;
        txttop.Text = txtresult.Text + btnOperator.Text;
        operator_clicked = btnOperator.Text;
        value = double.Parse(txtresult.Text);
        txtresult.Clear();
        }
        else
        {
            MessageBox.Show("Please enter any number");
        }
    }
}
}

0 个答案:

没有答案