数据库的单元测试主要是测试数据库中的数据是否符合特定的条件,Visual Studio 2010支持下面几种数据的单元测试类型(Visual Studio 2008 不支持数据库测试):
类型 |
说明 |
Data Checksum |
对数据进行Checksum检验 |
Empty ResultSet |
测试执行的SQL语句返回结果集是否为空 |
Execution Time |
测试执行时间 |
Expected Schema |
测试结果集中的列和数据类型是否与指定的测试条件匹配 |
Inconclusive |
默认缺省选项。单元测试里面的Inconclusive作用相同 |
Not Empty ResultSet |
与Empty ResultSet相反,测试结果集是否不为空 |
Row Count |
测试结果集是否包含指定的数据行数 |
Scalar Value |
测试返回的标量值是否与指定值相同。 |
更多官方解释及说明请参看:http://msdn.microsoft.com/en-us/library/aa833423.aspx
本文的例子可以使用下面的链接下载:
http://download.csdn.net/source/3014236
下面就开始Step By Step的建一个数据库单元测试实例。
1.新创建一个数据库测试文件:
<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="Picture_x0020_1" o:spid="_x0000_i1031" type="#_x0000_t75" alt="51.JPG" style='width:378pt;height:324.75pt;visibility:visible; mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image001.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image001.jpg" o:title="51" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
2.如果你是每一次创建数据库测试文件,那么系统会自动提示你连接一个数据库:
<!--[if gte vml 1]><v:shape id="Picture_x0020_18" o:spid="_x0000_i1030" type="#_x0000_t75" alt="52.JPG" style='width:324pt;height:413.25pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image002.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image002.jpg" o:title="52" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
3.创建成功后,如下图。点击去创建脚本文件。
<!--[if gte vml 1]><v:shape id="Picture_x0020_19" o:spid="_x0000_i1029" type="#_x0000_t75" alt="53.JPG" style='width:6in;height:162.75pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image003.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image003.jpg" o:title="53" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
4.如下图所示。UserGroup是我数据库中的一张表且里面只有一条数据。把系统默认的Inconclusive去掉,加上Execution Time和Row Count两个测试条件。注意看Value里面的解释。测试执行时间不能超过30秒,测试返回结果集只能返回0行。因为我们的测试结果集有一行数据,所以要修改rowCountCondition1的条件。
<!--[if gte vml 1]><v:shape id="Picture_x0020_20" o:spid="_x0000_i1028" type="#_x0000_t75" alt="54.JPG" style='width:6in;height:122.25pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image005.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image005.jpg" o:title="54" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
5. 在rowCountCondition1的属性框里面把0修改成1.如下:
<!--[if gte vml 1]><v:shape id="Picture_x0020_26" o:spid="_x0000_i1027" type="#_x0000_t75" alt="55.JPG" style='width:351pt;height:108pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image007.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image007.jpg" o:title="55" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
6.在Test List Editor里面刷新一下,就可以看到我们刚建的数据库测试了,执行。
<!--[if gte vml 1]><v:shape id="Picture_x0020_36" o:spid="_x0000_i1026" type="#_x0000_t75" alt="56.JPG" style='width:406.5pt;height:437.25pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image008.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image008.jpg" o:title="56" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
7.执行结果如下:
<!--[if gte vml 1]><v:shape id="Picture_x0020_38" o:spid="_x0000_i1025" type="#_x0000_t75" alt="57.JPG" style='width:247.5pt;height:187.5pt;visibility:visible;mso-wrap-style:square'> <v:imagedata src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image009.jpg" mce_src="file:///C:\DOCUME~1\victorg\LOCALS~1\Temp\msohtmlclip1\01\clip_image009.jpg" o:title="57" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
至此,数据库的单元测试界面操作基本完成。下面我们看一下后台代码。
通过代码我们可以了解到测试实际分为三个步骤:预测试(PretestAction),测试(TestAction),测试完成(PosttestAction)三部分。自动产生的代码,把预测试和测试完成的部分都省略了(
this.DatabaseTest1Data.PosttestAction = null;this.DatabaseTest1Data.PretestAction = null;) 实际操作中,我们可以根据自己的需要,进行修改。
如需转载,请注明本文原创自灰太狼的博客:http://blog.csdn.net/tjvictor