51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4525|回复: 3
打印 上一主题 下一主题

VS 中的unit test

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-8-27 15:45:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近遇到些困难,利用vs中的unit tet 进行对数据库设计代码测试,这个虽然自动生成了测试框架,但里面一些还是不会,比如返回DataSet expected =  DataSet actual 这了返回数据怎么来做

部分源码
/// 返回数据集
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandType"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public DataSet ExecuteQuery(string commandText, CommandType commandType, SqlParameter[] sqlParameters)
        {
            DataSet ds = new DataSet();

            using (SqlCommand cmd = new SqlCommand())
            {

                PrepareConnection(commandText, commandType, cmd, sqlParameters);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                try
                {
                    da.Fill(ds);
                }
                catch (Exception e)
                {
                    e.StackTrace.ToString();
                }
            }
            return ds;
        }

        /// <summary>
        /// 参数预编译
        /// </summary>
        /// <param name="commandText"></param>
        /// <param name="commandType"></param>
        /// <param name="cmd"></param>
        /// <param name="sqlParameters"></param>
        private void PrepareConnection(string commandText, CommandType commandType, SqlCommand cmd, SqlParameter[] sqlParameters)
        {
            SqlConnection conn = new SqlConnection(DbConnectionString);

            if (conn.State != ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                }
                catch (Exception e)
                {
                    e.StackTrace.ToString();
                }
            }
            cmd.CommandText = commandText;
            cmd.Connection = conn;
            cmd.CommandType = commandType;

            if (sqlParameters.Length >= 1)
            {
                foreach (SqlParameter sqlParameter in sqlParameters)
                {
                    if (sqlParameter.Value == null)
                    {
                        sqlParameter.Value = string.Empty;
                    }
                    cmd.Parameters.AddWithValue(sqlParameter.ParameterName, sqlParameter.Value);
                }
            }
        }
    }
}
生成测试代码
/// <summary>
        ///A test for ExecuteQuery
        ///</summary>
        [TestMethod()]
        public void ExecuteQueryTest()
        {
            DBAccessor target = new DBAccessor(); // TODO: Initialize to an appropriate value
            string commandText =@" SELECT * from UserData where [Alias]='tt'" ; // TODO: Initialize to an appropriate value
            CommandType commandType = CommandType.Text; // TODO: Initialize to an appropriate value
            SqlParameter[] sqlParameters = new SqlParameter[]{}; // TODO: Initialize to an appropriate value
            DataSet expected =  ; // TODO: Initialize to an appropriate value
            DataSet actual;
            actual = target.ExecuteQuery(commandText, commandType, sqlParameters);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for PrepareConnection
        ///</summary>
        [TestMethod()]
        [DeploymentItem("DBUtility.dll")]
        public void PrepareConnectionTest()
        {
            DBAccessor_Accessor target = new DBAccessor_Accessor(); // TODO: Initialize to an appropriate value
            string commandText = string.Empty; // TODO: Initialize to an appropriate value
            CommandType commandType = new CommandType(); // TODO: Initialize to an appropriate value
            SqlCommand cmd = null; // TODO: Initialize to an appropriate value
            SqlParameter[] sqlParameters = null; // TODO: Initialize to an appropriate value
            target.PrepareConnection(commandText, commandType, cmd, sqlParameters);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for DbConnectionString
        ///</summary>
        [TestMethod()]
        [DeploymentItem("DBUtility.dll")]
        public void DbConnectionStringTest()
        {
            string actual;
            actual = DBAccessor_Accessor.DbConnectionString;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
中的string commandText = string.Empty 等参数怎么进行设置呢?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2009-8-28 11:07:03 | 只看该作者

那位会的话帮我看看这个问题哦

那位会的话帮我看看这个问题哦。先行谢过啦
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 2009-9-15 14:05:09 | 只看该作者
我也是碰到这个问题 我判断它返回的不为空就行 我也觉得很牵强 大家一起交流吧
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2009-9-16 20:42:36 | 只看该作者
没 ,这个问题我到现在还不明白的,事情多,我没解决这个问题
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-14 23:58 , Processed in 0.072498 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表