单元测试:Nunit(四)
这部分我们重点看看数据库中CRUD的测试。首先要建立一个表UserInfo(ID(主键),UserName,Password,Age).
第一步:建立一个连接数据库的类,测试数据库的连接是否成功。
代码
public static SqlConnection GetConnection()
{
string sqlCon = "server=localhost;uid=sa;pwd=123456;database=NunitUser";
SqlConnection connection = new SqlConnection(sqlCon);
try
{
connection.Open();
}
catch
{
return null;
}
//返回连接对象
return connection;
}
{
string sqlCon = "server=localhost;uid=sa;pwd=123456;database=NunitUser";
SqlConnection connection = new SqlConnection(sqlCon);
try
{
connection.Open();
}
catch
{
return null;
}
//返回连接对象
return connection;
}
当然,这个数据库的连接我们也需要测试,看是否连接成功
[Test]
public void TestGetConnection()
{
SqlConnection conn = Connection.GetConnection();
//如果连接失败,就会返回null,只要判断是否为null,就可以断定是否连接成功
Assert.IsNotNull(conn);
}
public void TestGetConnection()
{
SqlConnection conn = Connection.GetConnection();
//如果连接失败,就会返回null,只要判断是否为null,就可以断定是否连接成功
Assert.IsNotNull(conn);
}
第二步:添加一个Person类
代码
public class Person
{
/// <summary>
/// 用户ID
/// </summary>
private int id;
/// <summary>
/// 用户标识
/// </summary>
public int ID
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 姓名
/// </summary>
private string userName;
/// <summary>
/// 姓名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 密码
/// </summary>
private string pwd;
/// <summary>
/// 密码
/// </summary>
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
/// <summary>
/// 年龄
/// </summary>
private int age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return age; }
set { age = value; }
}
{
/// <summary>
/// 用户ID
/// </summary>
private int id;
/// <summary>
/// 用户标识
/// </summary>
public int ID
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 姓名
/// </summary>
private string userName;
/// <summary>
/// 姓名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value; }
}
/// <summary>
/// 密码
/// </summary>
private string pwd;
/// <summary>
/// 密码
/// </summary>
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
/// <summary>
/// 年龄
/// </summary>
private int age;
/// <summary>
/// 年龄
/// </summary>
public int Age
{
get { return age; }
set { age = value; }
}
第三步:接下来开始实现CRUD功能,代码很简单,就不作详细的介绍了
代码
public class DBPerson
{
/// <summary>
/// 插入操作
/// </summary>
public void Create(Person person)
{
string sql = "insert into UserInfo(UserName,Password,Age) values (@UserName,@Password,@Age)";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
//定义参数,插值
command.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int));
command.Parameters["@UserName"].Value = person.UserName;
command.Parameters["@Password"].Value = person.Pwd;
command.Parameters["@Age"].Value = person.Age;
try
{
command.ExecuteNonQuery();
}
catch(System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
{
/// <summary>
/// 插入操作
/// </summary>
public void Create(Person person)
{
string sql = "insert into UserInfo(UserName,Password,Age) values (@UserName,@Password,@Age)";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
//定义参数,插值
command.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int));
command.Parameters["@UserName"].Value = person.UserName;
command.Parameters["@Password"].Value = person.Pwd;
command.Parameters["@Age"].Value = person.Age;
try
{
command.ExecuteNonQuery();
}
catch(System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
//不要忘记将数据库关闭
conn.Close();
}
}
/// <summary>
/// 修改操作
/// </summary>
public void Update(Person person)
{
string sql = "update UserInfo set UserName=@UserName,Password=@Password,Age=@Age where ID=@ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@UserName"].Value = person.UserName;
command.Parameters["@Password"].Value = person.Pwd;
command.Parameters["@Age"].Value = person.Age;
command.Parameters["@ID"].Value = person.ID;
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
conn.Close();
}
}
/// <summary>
/// 查看操作
/// </summary>
public Person GetById(int id)
{
string sql = "select * From UserInfo where ID = @ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@ID"].Value = id;
SqlDataReader reader = command.ExecuteReader();
Person person = null;
if (reader.Read())
{
person = new Person();
person.ID = id;
person.UserName = reader["UserName"].ToString();
person.Pwd = reader["Password"].ToString();
person.Age = Convert.ToInt32(reader["Age"]);
}
reader.Close();
conn.Close();
return person;
}
/// <summary>
/// 删除操作
/// </summary>
public void RemoveById(int id)
{
string sql = "delete from UserInfo where ID = @ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@ID"].Value = id;
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
}
conn.Close();
}
}
/// <summary>
/// 修改操作
/// </summary>
public void Update(Person person)
{
string sql = "update UserInfo set UserName=@UserName,Password=@Password,Age=@Age where ID=@ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@UserName"].Value = person.UserName;
command.Parameters["@Password"].Value = person.Pwd;
command.Parameters["@Age"].Value = person.Age;
command.Parameters["@ID"].Value = person.ID;
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
conn.Close();
}
}
/// <summary>
/// 查看操作
/// </summary>
public Person GetById(int id)
{
string sql = "select * From UserInfo where ID = @ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@ID"].Value = id;
SqlDataReader reader = command.ExecuteReader();
Person person = null;
if (reader.Read())
{
person = new Person();
person.ID = id;
person.UserName = reader["UserName"].ToString();
person.Pwd = reader["Password"].ToString();
person.Age = Convert.ToInt32(reader["Age"]);
}
reader.Close();
conn.Close();
return person;
}
/// <summary>
/// 删除操作
/// </summary>
public void RemoveById(int id)
{
string sql = "delete from UserInfo where ID = @ID";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
command.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
command.Parameters["@ID"].Value = id;
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
}
建立测试类
代码
[TestFixture]
public class DBPersonTest
{
/// <summary>
/// 数据库操作对象
/// </summary>
private DBPerson dbPerson;
[TestFixtureSetUp]
public void Init()
{
dbPerson = new DBPerson();
}
#region 测试创建
/*
* 测试的方法==>我们先插入一个数据,
* 然后获取刚插入的数据,比较两次数据是否一致,
* 如果一致,则表示插入操作正确
*/
[Test]
public void TestCreate()
{
//定义Person
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
int maxId = GetMaxId();
person.ID = maxId; //Person的Id
Person person2 = dbPerson.GetById(maxId);
Compare(person, person2);
//删除测试插入的数据
dbPerson.RemoveById(maxId);
}
#endregion
#region 测试修改
/*
* 测试方法==>因为在测试的时候,我们不需要让测试类之间有依赖关系,
* 同时也要保证测试修改类不能因为数据库中没有数据而发生错误,因此,
* 需要先插入数据,再将数据获取出来,修改以后比较是否一致
*/
[Test]
public void TestUpdate()
{
//UserInfo
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
//获取刚插入的ID
int maxId = GetMaxId();
//要修改成的UserInfo
Person personNew = new Person();
personNew.ID = maxId;
personNew.UserName = "wang";
personNew.Pwd = "duke";
personNew.Age = 21;
dbPerson.Update(personNew);
//得到修改后的Person对象
Person personRes = dbPerson.GetById(maxId);
Compare(personNew, personRes);
//删除记录
dbPerson.RemoveById(maxId);
}
#endregion
#region 测试删除
/*
* 测试方法==>插入数据,然后删除,根据插入后得到的ID去找数据,如果删除成功,返回null,
* 如果返回的不为空,表示删除失败
*/
[Test]
public void TestRemoveById()
{
//UserInfo
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
//获取刚插入的ID
int maxId = GetMaxId();
//删除数据
dbPerson.RemoveById(maxId);
Person personRes = dbPerson.GetById(maxId);
Assert.IsNull(personRes);
}
#endregion
/// <summary>
/// 获取刚刚插入的数据ID
/// </summary>
private int GetMaxId()
{
string sql = "select max(ID) as maxId From UserInfo";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
int maxId = 0;
if (reader.Read())
{
maxId = Convert.ToInt32(reader["maxId"]);
}
reader.Close();
conn.Close();
return maxId;
}
/// <summary>
/// 比较对象
/// </summary>
private void Compare(Person person1, Person person2)
{
Assert.AreEqual(person1.ID, person2.ID);
Assert.AreEqual(person1.UserName, person2.UserName);
Assert.AreEqual(person1.Pwd, person2.Pwd);
Assert.AreEqual(person1.Age, person2.Age);
}
}
public class DBPersonTest
{
/// <summary>
/// 数据库操作对象
/// </summary>
private DBPerson dbPerson;
[TestFixtureSetUp]
public void Init()
{
dbPerson = new DBPerson();
}
#region 测试创建
/*
* 测试的方法==>我们先插入一个数据,
* 然后获取刚插入的数据,比较两次数据是否一致,
* 如果一致,则表示插入操作正确
*/
[Test]
public void TestCreate()
{
//定义Person
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
int maxId = GetMaxId();
person.ID = maxId; //Person的Id
Person person2 = dbPerson.GetById(maxId);
Compare(person, person2);
//删除测试插入的数据
dbPerson.RemoveById(maxId);
}
#endregion
#region 测试修改
/*
* 测试方法==>因为在测试的时候,我们不需要让测试类之间有依赖关系,
* 同时也要保证测试修改类不能因为数据库中没有数据而发生错误,因此,
* 需要先插入数据,再将数据获取出来,修改以后比较是否一致
*/
[Test]
public void TestUpdate()
{
//UserInfo
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
//获取刚插入的ID
int maxId = GetMaxId();
//要修改成的UserInfo
Person personNew = new Person();
personNew.ID = maxId;
personNew.UserName = "wang";
personNew.Pwd = "duke";
personNew.Age = 21;
dbPerson.Update(personNew);
//得到修改后的Person对象
Person personRes = dbPerson.GetById(maxId);
Compare(personNew, personRes);
//删除记录
dbPerson.RemoveById(maxId);
}
#endregion
#region 测试删除
/*
* 测试方法==>插入数据,然后删除,根据插入后得到的ID去找数据,如果删除成功,返回null,
* 如果返回的不为空,表示删除失败
*/
[Test]
public void TestRemoveById()
{
//UserInfo
Person person = new Person();
person.UserName = "sa";
person.Pwd = "123456";
person.Age = 22;
//插入数据
dbPerson.Create(person);
//获取刚插入的ID
int maxId = GetMaxId();
//删除数据
dbPerson.RemoveById(maxId);
Person personRes = dbPerson.GetById(maxId);
Assert.IsNull(personRes);
}
#endregion
/// <summary>
/// 获取刚刚插入的数据ID
/// </summary>
private int GetMaxId()
{
string sql = "select max(ID) as maxId From UserInfo";
SqlConnection conn = Connection.GetConnection();
SqlCommand command = new SqlCommand(sql, conn);
SqlDataReader reader = command.ExecuteReader();
int maxId = 0;
if (reader.Read())
{
maxId = Convert.ToInt32(reader["maxId"]);
}
reader.Close();
conn.Close();
return maxId;
}
/// <summary>
/// 比较对象
/// </summary>
private void Compare(Person person1, Person person2)
{
Assert.AreEqual(person1.ID, person2.ID);
Assert.AreEqual(person1.UserName, person2.UserName);
Assert.AreEqual(person1.Pwd, person2.Pwd);
Assert.AreEqual(person1.Age, person2.Age);
}
}
代码中的注释很详细,就不用额外的针对代码说什么了。其实,对于Nunit测试,最大的弊端就是不能调试,写代码不可能一步就成功的,就像在写这个过程中,在Update中将数据库表名写错了,结果测试未通过,Nunit的IDE中提示的错误位置并不是你代码真正出错的地方,所以一般要仔细的找找错误发生的位置,一步一步的排查。