前面DebugLZQ寫了一篇博文,介紹的是如何使用Nunit編寫.NET單元測試。但是使用NUnti進行單元測試有一個致命的弱點:無法調試。因為我們的測試本省也是代碼,同樣我們不能確定我們的代碼是對的。這篇博文將以在VS2010下連接數據庫並插入一個字段的方法編寫單元測試為例,介紹如何使用TestDriven.NET彌補Nunit的這個致命的弱點。
示例項目的工程架構如下:
下面給出幾個主要類的源碼,Person類的代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VS2010建立SQLServer數據庫Test { public class Person { private int id; private string userName; private string password; public int Id { get { return id; } set { id = value; } } public string UserName { get { return userName; } set { userName = value; } } public string Password { get { return password; } set { password = value; } } } }
數據庫連接類Connection源碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Configuration; using System.Windows.Forms; namespace VS2010建立SQLServer數據庫Test { public class Connection { public static SqlConnection GetConnection() { string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Visual Studio 2010\VS2010建立SQLServer數據庫Test\VS2010建立SQLServer數據庫Test\Database1.mdf;Integrated Security=True;User Instance=True"; SqlConnection conn = new SqlConnection(connectionString); try { conn.Open(); } catch { return null; } return conn; } } }
相應的測試類ConnectionTest如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using NUnit.Framework;// namespace VS2010建立SQLServer數據庫Test { [TestFixture] public class ConnectionTest { [Test] public void GetConnectionTest() { SqlConnection conn= Connection.GetConnection(); Assert.IsNotNull(conn); } } }
數據庫幫助類:DBHelper如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace VS2010建立SQLServer數據庫Test { public class DBHelper { public static void Insert(Person person) { string sqlstring = "Insert into UserInfo(username,password) values(@username,@password)"; SqlConnection conn= Connection.GetConnection(); SqlCommand cmd = new SqlCommand(sqlstring, conn); cmd.Parameters.Add("@username", System.Data.SqlDbType.VarChar); cmd.Parameters.Add("@password", System.Data.SqlDbType.VarChar); cmd.Parameters["@username"].Value = person.UserName; cmd.Parameters["@password"].Value = person.Password; try { cmd.ExecuteNonQuery(); } catch { throw new Exception("unExpected exception!"); } finally { conn.Close(); } } /// public static Person GetPersonById(int id) { string sqlstring = "Select * from UserInfo where id=@id"; SqlConnection conn = Connection.GetConnection(); SqlCommand cmd = new SqlCommand(sqlstring, conn); cmd.Parameters.Add("@id", System.Data.SqlDbType.Int ); cmd.Parameters["@id"].Value = id; SqlDataReader reader= cmd.ExecuteReader(); Person person = null; if (reader.Read()) { person = new Person() { Id=id, UserName=reader["UserName"].ToString(), Password=reader["Password"].ToString() }; } reader.Close(); conn.Close(); return person; } } }
針對的表結果如下:
DBHelper的測試類DBHelperTest如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using NUnit.Framework; namespace VS2010建立SQLServer數據庫Test { [TestFixture] public class DBHelperTest { [Test ] public void InsertTest() { Person person = new Person() { UserName="DebugLZQ_A", Password="DebugLZQ_A" }; DBHelper.Insert(person); Person personResult=DBHelper.GetPersonById(GetMaxId()); Assert.AreEqual(person.UserName , personResult.UserName ); } private int GetMaxId() { string sqlString = "Select max(Id) as maxId from UserInfo"; SqlConnection conn = Connection.GetConnection(); SqlCommand cmd = new SqlCommand(sqlString, conn); SqlDataReader reader= cmd.ExecuteReader(); int maxId = 0; if (reader.Read()) { maxId = Convert.ToInt32(reader["maxId"]); } reader.Close(); conn.Close(); return maxId; } } }
代碼編寫工作完畢。
我們首先來看下,在NUnit下的測試結果:
看完DebugLZQ之前的博文應該可以到這一步了。NUnit功能固然強大,但是前面說過,我們使用NUnit是無法進行調試的,這是難以忍受的,代碼簡單的時候還好,代碼量打起來了以后,很難保證我們的測試是正確的,因此,調試我們的測試時很有必要的。
好的下面開始介紹解決這個問題的方法,今天的主角:TestDriven.NET。可到下面這個網站上去下載。
這段英文的介紹,我就不多說了。 重點是亮點:
1.以插件的形式“0摩擦”集成於VS。
2.它支持所有版本的VS
下載安裝完成以后,我們可以直接在測試的case、整個工程甚至整個解決方案(其實是任何位置)上面右擊選擇RunTest
可以在項目輸出框中查看相應的單元測試結果
如果我們需要進行調試,OK這樣做,Test With Debugger:
這樣我們可以很容以查看相關的調試信息
它還有一個很強大的功能:查看哪些代碼測試過,哪些沒有。如果這樣我們需要這樣做
這時我們可以在NCoverExplorer中查看相應的結果:
紅色的代碼表示沒有測試過,淺綠色的代碼表示我們已經測試過。並且我們可獲得其他詳細信息,比如代碼的測試率等。
我們也可以為我們的測試生成性能報告
就介紹的這里,謝謝大家的時間。
如果你覺得本文章對您有幫助,請點擊下面的“綠色通道”----“關注DebugLZQ”,共同交流進步~祝程序猿們身體健康,工作愉快~
---
Update: VS12、VS13版可借助NUnit Test Adapter集成到VS中去:
請參考DebugLZQ后續博文:Unit Test: Using NUnit with Visual Studio 2012 Unit Test Projects [NuGet]