使用 NUnit 的测试用例
各位,我是为这些方法编写测试用例的新手。这里我有一个 InsertMethod,我想使用 NUnit 测试框架为其编写测试用例。帮助我编写以下方法的测试用例:
public bool insertUser(String FirstName, String LastName) { 布尔结果=假;
SqlConnection myconn = new SqlConnection();
SqlCommand mycmd = new SqlCommand();
try
{
myconn.ConnectionString = "Data Source=BABU-PC;Initial Catalog=contacts;Integrated Security=True";
myconn.Open();
mycmd.Connection = myconn;
mycmd.CommandText = "InsertUser";
mycmd.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@FirstName";
param1.DbType = DbType.AnsiString;
param1.Size = 8000;
param1.Value = FirstName;
mycmd.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@LastName";
param2.DbType = DbType.AnsiString;
param2.Size = 8000;
param2.Value = LastName;
mycmd.Parameters.Add(param2);
int i = 0;
i = mycmd.ExecuteNonQuery();
if (i > 0)
{
result = true;
}
else
{
result = false;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
return false;
}
finally
{
mycmd.Dispose();
myconn.Close();
myconn = null;
}
return result;
}
谢谢
SBM
Folks, I am new to writing testcases for the methods. Here I have a InsertMethod for which I want to write testcase using NUnit testing framework. Help me in writing the testcase for the method below :
public bool insertUser(String FirstName, String LastName)
{
bool result = false;
SqlConnection myconn = new SqlConnection();
SqlCommand mycmd = new SqlCommand();
try
{
myconn.ConnectionString = "Data Source=BABU-PC;Initial Catalog=contacts;Integrated Security=True";
myconn.Open();
mycmd.Connection = myconn;
mycmd.CommandText = "InsertUser";
mycmd.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@FirstName";
param1.DbType = DbType.AnsiString;
param1.Size = 8000;
param1.Value = FirstName;
mycmd.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@LastName";
param2.DbType = DbType.AnsiString;
param2.Size = 8000;
param2.Value = LastName;
mycmd.Parameters.Add(param2);
int i = 0;
i = mycmd.ExecuteNonQuery();
if (i > 0)
{
result = true;
}
else
{
result = false;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
return false;
}
finally
{
mycmd.Dispose();
myconn.Close();
myconn = null;
}
return result;
}
Thanks
SBM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
细胞鱼是对的。不要考虑代码,而要考虑该方法应该做什么。甚至不看你的代码,我就会做这样的测试:
1-准备
然后尝试获取具有名字和姓氏的用户,并确保它不存在。
2- 执行
3- 检查
Cellfish is right. Don't think about the code, think about what the method is supposed to do. Without even looking at your code, I'd do this kind of test:
1- Preparation
Then try to fetch a user with firstName and lastName and make sure that it's not already there.
2- Execute
3- Check