浅谈NUnit单元测试方法小总结(3)

发表于:2011-12-30来源:博客园作者:韩迎龙点击数: 标签:NUnit单元测试
try { connection.Open(); } catch { return null; } return connection; } } 在ConnectionTest测试类中输入如下代码,测试连接是否成功 [TestFixture] public class ConnectionTest { [Test] p

  try

  {

  connection.Open();

  }

  catch

  {

  return null;

  }

  return connection;

  }

  }

  在ConnectionTest测试类中输入如下代码,测试连接是否成功

  [TestFixture]

  public class ConnectionTest

  {

  [Test]

  public void TestGetConnection()

  {

  SqlConnection conn = connection.GetConnection();

  Assert.IsNotNull(conn); //判断其不为空

  }

  }

  对数据库增删改查的测试代码如下:新建DBperson类实现数据库增删改查的功能

  public void Insert(Person person)

  {

  string sqlStr = "insert into person(username,password,age)values(@username,@password,@age)";

  SqlConnection conn = connection.GetConnection();

  SqlCommand command = new SqlCommand(sqlStr, 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.Password;

  command.Parameters["@age"].Value = person.Age;

  try

  {

  command.ExecuteNonQuery(); //返回受命令影响的行数

  }

  catch (Exception ex)

  {

  Console.WriteLine(ex.Message);

  }

  finally

  {

  conn.Close();

  }

  }

  public void Update(Person person)

  {

  string sqlStr = "update person set username=@username,password=@password,age=@age where id=@id";

  SqlConnection conn = connection.GetConnection();

  SqlCommand command = new SqlCommand(sqlStr, 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.Password;

  command.Parameters["@age"].Value = person.Age;

  command.Parameters["@id"].Value = person.Id;

  try

  {

  command.ExecuteNonQuery();

  }

  catch(Exception ex)

  {

  Console.WriteLine(ex.Message);

  }

  finally

  {

  conn.Close();

  }

  }

  public Person GetByID(int id)

  {

  string sqlStr = "select * from person where id=@id";

  SqlConnection conn = connection.GetConnection();

  SqlCommand command = new SqlCommand(sqlStr, 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.Password = reader["password"].ToString();

  person.Age = Convert.ToInt32(reader["age"]);

  }

  reader.Close();

  conn.Close();

  return person;

  }

  public void RemoveID(int id)

  {

  string sqlStr = "delete from person where id=@id";

  SqlConnection conn = connection.GetConnection();

  SqlCommand command = new SqlCommand(sqlStr, 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();

  }

  }

  在新建一个测试类:DBPerson类的测试代码如下:

  namespace NunitTest

  {

  [TestFixture]

  public class DBPersonTest

原文转自:http://www.ltesting.net