一个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 未返回所需的值。");
        }


    }


}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值