【发布时间】: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