首页 新闻 赞助 找找看

关于VSTS中做Web程序单元测试的严重问题?

0
悬赏园豆:100 [已关闭问题]
我头一次做测试,根本不会搞。我们的程序是Web程序,ASP.NET &nbsp; 2.0的,发现我用的VSTS中自带的NUint很好用,能自动生成测试代码,挺激动,一下子就把所有类中的方法都生成了测试代码,但是遇到两个很严重的问题: <BR><BR>1、测试一个数据库访问类时,自动生成的测试代码会自动实例化一个数据库访问类,DataAccess &nbsp; target &nbsp; = &nbsp; new &nbsp; DataAccess(); &nbsp; ,但是在获取数据库链接字符串target.DbConnection的时候,就会获取不到User &nbsp; Id、Password和Data &nbsp; Source,都是空的。因为这个网站需要登录后才能访问,并且数据库连接用的User &nbsp; Id、Password和Data &nbsp; Source是放在web.config里面的 &lt;appSettings&gt; 下面的,所以我感觉问题应该是自动生成的测试代码没有到web.config中获取此三项数值所致。现在我怎么才能让自动生成的代码能跟web.config关联上以便获取到相应的值? <BR><BR>怕说不明白,特附代码如下: <BR>自动生成的测试代码: <BR>[TestMethod()] <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public &nbsp; void &nbsp; getDataTableTest() <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataAccess &nbsp; target &nbsp; = &nbsp; new &nbsp; DataAccess(); <BR><BR>string &nbsp; conn=target.DbConnection; &nbsp; ///这句话是我自己加的,此处有断点,目的是测试是否能获取到正确的User &nbsp; Id、Password和Data &nbsp; Source,这里过不去,下面的target.getDataTable(strSql)就肯定跑不过去了。 <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string &nbsp; strSql &nbsp; = &nbsp; "select &nbsp; count(yhm) &nbsp; from &nbsp; yhdlrz &nbsp; where &nbsp; yhm= 'dfsh ' "; &nbsp; // &nbsp; TODO: &nbsp; 初始化为适当的值 <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable &nbsp; expected &nbsp; = &nbsp; null; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable &nbsp; actual; <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actual &nbsp; = &nbsp; target.getDataTable(strSql); <BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.AreEqual(expected, &nbsp; actual, &nbsp; "DoubleFox.DAL.DataAccess.getDataTable &nbsp; 未返回所需的值。 "); <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.Inconclusive( "验证此测试方法的正确性。 "); <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <BR>注:预期结果值expected &nbsp; 我已经构造好了,现在就是他自动实例化的target中,target.DbConnection获取不到值。下面是DataAccess类的相关代码: <BR><BR>/// &nbsp; 数据库连接类,负责建立数据库连接和对数据的一些访问操作 <BR>public &nbsp; class &nbsp; DataAccess <BR>{ <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public &nbsp; OracleConnection &nbsp; DbConnection <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &nbsp; getOracleConnection(); <BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &
风行的主页 风行 | 初学一级 | 园豆:100
提问于:2007-11-08 14:20
< >
分享
其他回答(4)
0
代码没有贴全啊
麒麟.NET | 园豆:3614 (老鸟四级) | 2007-11-12 17:21
0
楼主估计不知道NUnit是啥东西吧~~~;)VSTS自带的那个反正不是,不过呢在NUnit中读取配置文件这种工作还是可能做到的,只需要把web.config复制一份到待测试的工程下面,然后改成 工程名.dll.config 就可以保证程序正确读取config文件,不过VS自带的下面的忘记了,机制应该类似,但是如果需要依赖HttpContext.Current这样只有在有用户上下文对象存在时才生效的东西的方法我也不知道如何才能作测试?哪个牛人知道~~?
沙加 | 园豆:3680 (老鸟四级) | 2007-11-12 20:31
0
用这个工具吧 挺好的Web Application Stress Tool 下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyID=e2c0585a-062a-439e-a67d-75a89aa36495&displaylang=en
坏坏@net | 园豆:280 (菜鸟二级) | 2007-11-13 20:44
0
估计VSTS再牛也不可能知道你的数据库配置吧! 不过它肯定是提供了自定义配置的功能,没玩过VSTS呢,自己找找再那配吧!估计不会很难找
Justin | 园豆:980 (小虾三级) | 2007-11-14 22:07
0
使用NUnit测试时 如果是在测试项目中 要读取appSettings或connectionStrings 可以新建一个Application Configuration File(app.config) 把web.config的相关配置拷贝到app.config中就可以读取了 需要注意,dll所在路径和相关的文件如XML等路径的不同。
Anders Cui | 园豆:1570 (小虾三级) | 2007-11-19 13:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册