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

发表于:2011-12-30来源:博客园作者:韩迎龙点击数: 标签:NUnit单元测试
{ private DBPerson dbperson; [TestFixtureSetUp] public void Init() { dbperson = new DBPerson(); } [Test] public void TestInsert() { Person person = new Person(); person.Username = 韩迎龙; person.Pa

  {

  private DBPerson dbperson;

  [TestFixtureSetUp]

  public void Init()

  {

  dbperson = new DBPerson();

  }

  [Test]

  public void TestInsert()

  {

  Person person = new Person();

  person.Username = "韩迎龙";

  person.Password = "citsoft";

  person.Age = 21;

  dbperson.Insert(person);

  int maxid = this.GetMaxId();

  person.Id = maxid;

  Person person2 = dbperson.GetByID(maxid);

  this.Company(person, person2);

  //做完测试后记得删除数据库中的数据

  dbperson.RemoveID(maxid);

  }

  [Test]

  public void TestUpdate()

  {

  Person person = new Person();

  person.Username = "韩鑫";

  person.Password = "hanyinglong";

  person.Age = 20;

  dbperson.Insert(person);

  int maxid = this.GetMaxId();

  Person person2 = dbperson.GetByID(maxid);

  person2.Username = "韩迎龙";

  person2.Password = "citsoft";

  person2.Age = 20;

  dbperson.Update(person2);

  Person person3 = dbperson.GetByID(maxid);

  this.Company(person2, person3);

  dbperson.RemoveID(maxid); //清空数据库

  }

  [Test]

  public void TestGetByID()

  {

  Person person = new Person();

  person.Username = "韩迎龙";

  person.Password = "citsoft";

  person.Age = 21;

  dbperson.Insert(person);

  int maxid = this.GetMaxId();

  person.Id = maxid;

  Person person2 = dbperson.GetByID(maxid);

  this.Company(person, person2);

  dbperson.RemoveID(maxid);

  }

  [Test]

  public void TestRemoveByID()

  {

  Person person = new Person();

  person.Username = "韩迎龙";

  person.Password = "citsoft";

  person.Age = 21;

  dbperson.Insert(person);

  int maxid = this.GetMaxId();

  dbperson.RemoveID(maxid);

  Person person2 = dbperson.GetByID(maxid);

  Assert.IsNull(person2);

  }

  private int GetMaxId()

  {

  string sql = "select max(id) as maxID from person";

  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"]);

  //Console.WriteLine(reader["maxID"]);

  }

  reader.Close();

  conn.Close();

  return maxID;

  }

  private void Company(Person person1, Person person2)

  {

  Assert.AreEqual(person1.Id, person2.Id);

  Assert.AreEqual(person1.Username, person2.Username);

  Assert.AreEqual(person1.Password, person2.Password);

  Assert.AreEqual(person1.Age, person2.Age);

  }

  }

  }

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