【问题标题】:How do you know whether to automate testing or use manual?你怎么知道是自动化测试还是使用手动?
【发布时间】:2018-12-06 18:14:36
【问题描述】:

我为医生管理系统制作了登录屏幕和菜单。用户登录并被发送到主菜单,根据角色类型,他们在每个子菜单上有不同的控制。

一切正常,现在是时候进行测试了。

我从未做过自动化测试——单元、集成或端到端。

据我了解,最好将业务逻辑与表示逻辑分开,只对业务逻辑进行单元测试?

考虑到这一点,按钮可见性和消息框是呈现逻辑,对吧?并且最适合我将验证功能的手动测试计划?对吗?

如果登录成功,我确定我应该进行单元测试?但是,该方法不依赖于数据库吗?那是集成测试吗?

这是我的代码

登录表单

 private void btnLogin_Click(object sender, EventArgs e)
    {


        //Try and open a connection with database and run the code
        try
        {

            //Create new instance of sql connection, pass in the connection string for BayOneSurgerySystem.mdf to connect to database.
            SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\davie\Documents\UniWork\Software Engineering\BayOneSurgerySystem1.0\Database\BayOneSystem.mdf;Integrated Security=True;Connect Timeout=30");

            //Create new instance of SQlCommand and pass in a query to be called to retrieve table data for username and passwords aswell as the connection object.
            SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Username = @username and Password = @password", conn);
            //This passes user input into @username and @password
            cmd.Parameters.AddWithValue("@username", txtBoxUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtBoxPassword.Text);

            //Open connection with database
            conn.Open();

            //Create new instance of dataSet to hold the data retrieved from sql query
            DataSet ds = new DataSet();
            //Create new instance of DataAdpater to retrieve the data pass in Sql command
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            //using DataAdapter fill in dataSet wiht data if user input and stored data matches
            da.Fill(ds);

            //Close the connection now data table is filled with username and password
            conn.Close();

            //declare bool, true if there is a match with database and user input
            bool loginSuccess = (ds.Tables[0].Rows.Count == 1);


            //if login success is true then open menu
            if (loginSuccess)
            {
                /*Change state of enum RoleTypes based on result from dataSet Role_ID column.
                 In UserRole table records are as follows: 
                 Role_ID 1 = PracticeManager
                 2 = Doctor
                 3 = Receptionist*/

                //Print role_ID to console to check that is been set.
                Console.WriteLine(ds.Tables[0].Rows[0]["Role_ID"]);

                try
                {
                    //Condition for the switch statement is: check Role_ID from UserRoles table
                    switch (ds.Tables[0].Rows[0]["Role_ID"])
                    {
                        //if the case is that Role_ID for the user logged in is 1 then run the function etc.
                        case 1:
                            {
                                Roles.Role = Roles.RoleType.practiceManager;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        case 2:
                            {
                                Roles.Role = Roles.RoleType.doctor;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        case 3:
                            {
                                Roles.Role = Roles.RoleType.receptionist;
                                Console.WriteLine("Role type changed to " + Roles.Role);
                            }
                            break;
                        default:
                            break;

                    }
                }//Switch condition cannot be reached then catch exception and print to console.
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }


                Console.WriteLine("Logged in.");
                FrmMenu menu = new FrmMenu();
                menu.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Invalid username or password.", "Error!", MessageBoxButtons.RetryCancel);
                Console.WriteLine("Not logged in");
            }


        }

        //If connection cant be opened diplsay error message and catch exception and print to console
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            MessageBox.Show("Sorry can't connect");
        }



    }

}

}

角色类

 public static class Roles
{
    /*The RoleType enum is declared here and changed in form login. RoleType is the condition for button visibility
    I.e if roletype is doctor, show doctor buttons*/
    public static RoleType Role;
    public enum RoleType
    {
        practiceManager,
        doctor,
        receptionist

    }


}

患者表格

//if this is clicked, display yesNo messagebox. If yes, logout.
    private void btnLogout_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            FrmLogin logout = new FrmLogin();
            logout.Show();
            this.Close();
        }
    }

    private void FrmPatients_Load(object sender, EventArgs e)
    {

        /*Buttons are visible depending on access level. Permissions are as follows:
         * Practice Manager - Patient Records
           Doctor - Register Patient, Patient notes
           Receptionist - New Patient, Register Patient*/
        if (Roles.Role == Roles.RoleType.practiceManager)
        {
            this.btnNewPatient.Visible = false;
            this.btnNotes.Visible = false;
            this.btnSignIn.Visible = false;

        }

        else if (Roles.Role == Roles.RoleType.doctor)
        {
            this.btnNewPatient.Visible = false;
        }
        else
        {
            this.btnNotes.Visible = false;
        }

    }

    //if this is clicked return to main menu
    private void pcBxBack_Click(object sender, EventArgs e)
    {
        FrmMenu menu = new FrmMenu();
        this.Close();
        menu.Show();
    }
}

}

测试类

 [TestClass]

     public class LoginTests
     {
       [TestMethod]
         public void Constuctor_NormalData_Login_Is_Successful()
         {

            //Arrange
            //Act
            //Assert
          }


        [TestMethod]
        public void Constructor_RoleTypeEnum_RoleType_Changes()
        {
           //Arrange
           //Act
           //Assert

         }


       }

关于您将使用自动化测试做什么的任何建议?

我有一个手动表,其中包含 21 个测试来验证所有消息框、按钮单击事件和按钮可见性。

对于 A,我真的需要一些自动化测试。如您所见,我有用于测试角色类型枚举和登录成功的模板,这是我能看到的唯一两种明显的测试方法。

提前致谢!

【问题讨论】:

    标签: c# winforms automated-tests


    【解决方案1】:

    在你的情况下,避免单元测试并进行端到端测试。


    您的问题非常模糊 - 我们在 Stack Overflow 上尽量避免这种情况,因为它很难回答。最好的答案是模糊的。在最坏的情况下,问题将被投票结束,提问者(即你)会失去能量。

    话虽如此,我还是要一个模糊的答案。

    查看您的btnLogin_Click 方法,我发现您没有将业务逻辑与用户界面分离。所述方法知道如何连接到数据库以及如何本地查询它,它知道更新它的用户界面,还知道如何登录到控制台。它可能知道更多,但这足以让我建议您反对单元测试。
    简单地说,单元测试是一种验证机器中的齿轮外观和行为是否符合您要求的方法。
    您的机器将所有齿轮和杠杆以及冲刺和主轴混合在一起。我并不是说您的代码不好。但如果你想进行单元测试,你将很难过。

    改为进行端到端测试
    Visual Studio 有办法automate applications。 (我不知道你是否必须有一个特殊的许可证级别,因为微软在解释 Visual Studio 的功能时“忘记”描述这一点。
    然后是Sikuli
    然后还有更多。
    我不推荐上面的或任何其他的,但这必须是你的选择。四处挖掘并测试以找到您喜欢的。许多是 F/OSS。

    HTH
    祝黑客愉快!

    【讨论】:

    • 这很有帮助!那正是我所想。 端到端测试是我需要的,太棒了。试图弄清楚如何使用 Arrange, Act, Assert,同时知道登录方法依赖于数据库。不过,你让我走上了正确的道路!我正在查看 Coded UI 测试,因为我有 Visual Studio Enterprise。我想知道我的大学是否没有企业会导致难以在 uni 中运行我的程序或参加考试?
    猜你喜欢
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多