一個C#寫單元測試的例子(結合DAAB)

出處:http://blog.niwota.com/a/63746.htm

// 測試所有者應該檢查每個測試的有效性。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using RightsManage;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;


namespace TestRightsManage
{
    /// <summary>
    ///這是 RightsManage.DepartmentManage 的測試類,旨在
    ///包含所有 RightsManage.DepartmentManage 單元測試
    ///</summary>
    [TestClass()]
    public class DepartmentManageTest
    {
        private TestContext testContextInstance;
        enum ValueType
        {
            vShort = 0,
            vInt,
            vString,
            vDate
        }

        static private bool CheckRecordExist(string strTable, string strField, string strValue, ValueType sType)
        {
            string strSQL = null;
            Int32 n = 0;
            Database m_db = DatabaseFactory.CreateDatabase("oracleConnectionString");
            switch (sType)
            {
                case ValueType.vShort:
                    {
                        strSQL = String.Format("select count({0}) from {1} where {0} = {2}", strField, strTable, strValue);
                        break;
                    }
                case ValueType.vInt:
                    {
                        strSQL = String.Format("select count({0}) from {1} where {0} = {2}", strField, strTable, strValue);
                        break;
                    }
                case ValueType.vString:
                    {
                        strSQL = String.Format("select count({0}) from {1} where {0} = '{2}'", strField, strTable, strValue);
                        break;
                    }
                case ValueType.vDate:
                    {
                        strSQL = String.Format("select count({0}) from {1} where {0} = '{2}'", strField, strTable, strValue);
                        break;
                    }
                default:
                    {
                        strSQL = String.Format("select count({0}) from {1} where {0} = '{2}'", strField, strTable, strValue);
                        break;
                    }
            }

            DbCommand dbcomm = m_db.GetSqlStringCommand(strSQL);
            Object obj = m_db.ExecuteScalar(dbcomm);
            n = Convert.ToInt32(obj);

            if (n < 1) return false;
            return true;

        }
        /// <summary>
        ///獲取或設置測試上下文,上下文提供
        ///有關當前測試運行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        #region 附加測試屬性
        //
        //編寫測試時,可使用以下附加屬性:
        //
        //使用 ClassInitialize 在運行類中的第一個測試前先運行代碼
        //
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在運行完類中的所有測試後再運行代碼
        //
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在運行每個測試前先運行代碼
        //
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在運行完每個測試後運行代碼
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///AddDepartment (string, string, string) 的測試
        ///</summary>
        [TestMethod()]
        public void AddDepartmentTest()
        {
            string strCode = "0101"; // TODO: 初始化爲適當的值
            string strName = "軟件部"; // TODO: 初始化爲適當的值
            string strDesc = "專門開發軟件"; // TODO: 初始化爲適當的值
            DepartmentManage target = new DepartmentManage();
            bool expected = true;
            bool actual;
            actual =  target.AddDepartment(strCode, strName, strDesc);
            if (actual) Console.WriteLine("第一條記錄整進去了。");

            strCode = "0102"; // TODO: 初始化爲適當的值
            strName = "宣傳部"; // TODO: 初始化爲適當的值
            strDesc = "專門出去吹牛皮"; // TODO: 初始化爲適當的值
            actual &= target.AddDepartment(strCode, strName, strDesc);
            if (actual) Console.WriteLine("第二條記錄也整進去了。");

            strCode = "0103"; // TODO: 初始化爲適當的值
            strName = "公關部"; // TODO: 初始化爲適當的值
            strDesc = "專門從事與訂單相關的特殊活動"; // TODO: 初始化爲適當的值
            actual &= target.AddDepartment(strCode, strName, strDesc);
            if (actual) Console.WriteLine("又整進去了一條記錄,正在驗證數據庫中的數據。");

            actual &= CheckRecordExist("test_department", "code", "0101", ValueType.vString);
            if (actual) Console.WriteLine("第一條記錄確實存在。");
            actual &= CheckRecordExist("test_department", "code", "0102", ValueType.vString);
            if (actual) Console.WriteLine("第二條記錄也確實存在。");
            actual &= CheckRecordExist("test_department", "code", "0103", ValueType.vString);
            if (actual) Console.WriteLine("第三條記錄還是存在。");

            Assert.AreEqual(expected, actual, "RightsManage.DepartmentManage.AddDepartment 搞了半天沒有把數據整進去。");
            //Assert.Inconclusive("驗證此測試方法的正確性。");
            //Assert.IsTrue()
        }

        /// <summary>
        ///DeleteDepartmentByCode (string) 的測試
        ///</summary>
        [TestMethod()]
        public void DeleteDepartmentByCodeTest()
        {
            DepartmentManage target = new DepartmentManage();

            string strCode = "0101"; // TODO: 初始化爲適當的值

            bool expected = false;
            bool actual;

            actual = target.DeleteDepartmentByCode(strCode);
            if(actual)Console.WriteLine("刪除第一條記錄返回成功!!");
            actual = CheckRecordExist("test_department", "code", "0101", ValueType.vString);
            Assert.AreEqual(expected, actual, "RightsManage.DepartmentManage.DeleteDepartmentByCode 數據庫中的數據沒有被刪除掉。");
            Console.WriteLine("數據庫中確實不存在第一條記錄了!!");
        }

        /// <summary>
        ///ModifyDepartmentByCode (string, string, string) 的測試
        ///</summary>
        [TestMethod()]
        public void ModifyDepartmentByCodeTest()
        {
            DepartmentManage target = new DepartmentManage();

            string strCode = "0102"; // TODO: 初始化爲適當的值

            string strNewName = "西瓜不爛"; // TODO: 初始化爲適當的值

            string strNewDesc = "歪棗裂瓜七上八下"; // TODO: 初始化爲適當的值

            bool expected = true;
            bool actual;

            actual = target.ModifyDepartmentByCode(strCode, strNewName, strNewDesc);
            if (actual) Console.WriteLine("調用ModifyDepartmentByCode返回成功");
            actual &= CheckRecordExist("test_department", "name", "西瓜不爛", ValueType.vString);
            if (actual) Console.WriteLine("數據庫中確實存在西瓜不爛的這麼一條記錄");

            Assert.AreEqual(expected, actual, "RightsManage.DepartmentManage.ModifyDepartmentByCode 未返回所需的值。");
        }


        /// <summary>
        ///DeleteDepartmentByName (string) 的測試
        ///</summary>
        [TestMethod()]
        public void DeleteDepartmentByNameTest()
        {
            DepartmentManage target = new DepartmentManage();

            string strName = "西瓜不爛"; // TODO: 初始化爲適當的值

            bool expected = true;
            bool actual;

            actual = target.DeleteDepartmentByName(strName);

            strName = "公關部";
            actual &= target.DeleteDepartmentByName(strName);

            Assert.AreEqual(expected, actual, "RightsManage.DepartmentManage.DeleteDepartmentByName 未返回所需的值。");
        }


    }


}

 
發佈了2 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章