您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > Nunit
使用NUnit測(cè)試Private和Protected方法
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/12 15:57:51 ] 推薦標(biāo)簽:

下面是測(cè)試類代碼:
using System;
using System.Reflection;

using NUnit.Framework;

namespace ClassLibrary1
{
    /**//// <summary>
    /// Summary description for Tester.
    /// </summary>
    [TestFixture]
    public class Tester
    {
        [Test]
        public void MyPrivateMethod_Test()
        {
            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1();
            object[] aobjParams = new object[] { 3, 4 };
            object strRet;
            strRet = RunInstanceMethod( typeof(ClassLibrary1.Class1),
                "MyPrivateMethod",
                class1,
                aobjParams
            );
            Assert.AreEqual(7, strRet.ToString());
        }

        /**//// <summary>
        /// 運(yùn)行靜態(tài)方法
        /// </summary>
        /// <param name="t"></param>
        /// <param name="strMethod"></param>
        /// <param name="aobjParams"></param>
        /// <returns></returns>
        public static object RunStaticMethod(System.Type t, string strMethod,
            object [] aobjParams)
        {
            BindingFlags eFlags =
                BindingFlags.Static | BindingFlags.Public |
                BindingFlags.NonPublic;
            return RunMethod(t, strMethod,
                null, aobjParams, eFlags);
        }

        /**//// <summary>
        /// 運(yùn)行實(shí)例方法
        /// </summary>
        /// <param name="t"></param>
        /// <param name="strMethod"></param>
        /// <param name="objInstance"></param>
        /// <param name="aobjParams"></param>
        /// <returns></returns>
        public static object RunInstanceMethod(System.Type t, string strMethod,
            object objInstance, object [] aobjParams)
        {
            BindingFlags eFlags = BindingFlags.Instance | BindingFlags.Public |
                BindingFlags.NonPublic;
            return RunMethod(t, strMethod,
                objInstance, aobjParams, eFlags);
        }

        /**//// <summary>
        /// 運(yùn)行自定義方法
        /// </summary>
        /// <param name="t"></param>
        /// <param name="strMethod"></param>
        /// <param name="objInstance"></param>
        /// <param name="aobjParams"></param>
        /// <param name="eFlags"></param>
        /// <returns></returns>
        private static object RunMethod(System.Type t, string
            strMethod, object objInstance, object [] aobjParams, BindingFlags eFlags)
        {
            MethodInfo m;
            try
            {
                m = t.GetMethod(strMethod, eFlags);
                if (m == null)
                {
                    throw new ArgumentException("There is no method '" +
                        strMethod + "' for type '" + t.ToString() + "'.");
                }
                               
                object objRet = m.Invoke(objInstance, aobjParams);
                return objRet;
            }
            catch
            {
                throw;
            }
        }

    } // end of class

} // end of namespace

上一頁12下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd