|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
对于insert和delete,event中包含了插入/删除的记录的所有字段的值(太爽了。。)
IntroducingTSQLUnit
TSQLUnit简介
TSQLUnitisanopensourceunittestingframeworkforT-SQLwrittenbyHenrikEkelundandavailablefromhttp://sourceforge.net/projects/tsqlunit.HeresanexampleofhowIveusedit.
MyTSQLUnitteststakeasimilarpatternofthreeparts:
1)unittestsetup,测试设置
2)executionofthetargetprocedure,and实行测试方针存储历程.
3)checkingresults.反省了局.
Intheunittestsetup,IoftenchecktomakesuresomeonehasntdonebadthingstomydatawhenIwasntlooking:
在单位测试的设置中我常常反省其别人没有损坏我但愿的数据:
DECLARE@nIdINT,@nNewIdINT—-@nNewIdisforlater
SELECT@nId=[ID]FROMMyTableWHEREMyField=whatever
IF@nIdISNULL--or@@ROWCOUNT=0
EXECtsu_failureThedatahaschanged.whatevercouldntbefound
TheIFblockchecksfortheexpectedrecord.Ifitcouldntbefound,thetestfailsandwillgenerateanerrormessage.Thetestframeworkmovesontothenextunittest.Youdontneedtousethenameoftheunittestinthefailuremessagestring,becauseTSQLUnitwillnameitforyouwhenthetestfails.
IF块反省希冀的纪录.假如没法找到,测试失利并天生一个毛病动静.测试框架挪动到下一个单位测试.你不必在失利的动静中利用测试的名字,由于TSQLUnit会在测试失利时定名的.
NowIcallthestoredprocedureImabouttowrite:
如今我挪用我要写的存储历程:
EXECCreateMyTableNewRec@nId,@nNewIdOUTPUT
Asyoucansee,IvedeterminedthatIneedanoutputparameterfromthisnewprocedure.Incheckingtheresults,Imakesuretheoutputparameterreallyisfilledwithsomething:
你看,我将反省存储过程当中必要前往的参数.在反省了局中,要确认输入参数真的被添补了.
IF@nNewIdISNULL
EXECtsu_failureAnewrecordwasnotcreatedfortableMyTable.
IcouldfurthercheckthevaluetoseeifthenewrecordwascreatedinthewayIwantedittobecreated.
我能够反省更深条理的值,假如我请求的新纪录被创立了.
EachTSQLUnittestisitselfastoredprocedure.Listing1showswhatonelookslikewhenallofthepiecesareputtogether:
每一个TSQLUnit测试都是一个存储历程.列表1显现了一切测试段落在一同的情形.
Listing1.AcompleteunittestforT-SQL.
CREATEPROCEDUREut_MyTable_NewRec
AS
--==Setup==--
DECLARE@nIDINT,@nNewIdINT
SELECT@nId=IDFROMMyTable
WHEREMyField=whatever
IF@nIdISNULL--or@@ROWCOUNT=0
EXECtsu_failureThedatahaschanged.
Whatevercouldntbefound
--==Execute==--
EXECCreateMyTableNewRec@nId,@nNewIdOUTPUT
--==Check==--
IF@nNewIdISNULL
EXECtsu_failureAnewrecordwasnotcreated
fortableMyTable.
GO
Notethethree-partnameofthestoredprocedure,ut_MyTable_NewRec.Theprefix"ut_"alertsTSQLUnitthatthisisaunittestitshouldrun.Ifyoualreadyusethisprefixut_forotherpurposes,TSQLUnitletsyousetittosomethingelse."MyTable"isthenameofagroupofrelatedunittests,knownasasuiteoftests.Forinstance,youcouldaddanotherunittestcalledut_MyTable_DeleteRec.TheMyTablesuitewouldtestbothaddinganddeletingarecordtoMyTable.Thesuitecanberunseparatelyfromothertestsuites.Thethirdpartofthename–"NewRec"or"DeleteRec"–uniquelyidentifiesthisunittest.
三个段落的存储历程,前缀ut_是告知TSQLUnit要运转的一个单位测试.假如你已利用ut_前缀作其他的用处,TSQLUnit请求你修正为其他的名字.名字的第二个段落,显现了测试的组,比方,你能够到场一个单位测试名字为ut_MyTable_DeleteRec.这个组会测试增加和删除一个纪录到MyTable,组也能够分隔到其他的测试组中.名字的第三个段落,是测试的独一标示.
NotethatyoudontneedBEGINTRANandROLLBACKineachunittest;TSQLUnittakescareofthisforyou.
你也不在必要BEGINTRAN和ROLLBACK在每一个单位测试.TSQLUnit卖力为你处置
Runningtheunittest
运转单位测试
InordertoruntheunittestinListing1,youneedtosetuptheframework.FromQueryAnalyzer,runtsqlunit.sqlonyourdevelopmentdatabase.Youneeddothisonlyonceforthedatabase.Next,createprocedureut_MyTable_NewRec,ifyouhaventalready.Nowyoureset.Simplyexecutetheunittest:
为了运转列表1中的单位测试,你要设置测试框架.在查询剖析器中,运转TSQLUnit.SQL在你的开辟数据库中..你仅仅要实行一次在数据库中.创立ut_myTable_newRec,如今你能够复杂的实行单位测试了:
--ThiswillrunalltestsforsuiteMyTable,这将运转MyTable组的一切测试
EXECtsu_RunTestsMyTable
Fixtures设备
SupposeIwantnumerousrecordstobeavailableforalltheunittestsofasuite.Idontwanttowritethesamesetupcodeforeachtest.TSQLUnitsolvestheproblemwithasetupfixture.Thecodeinthefixturewillberunbeforeeachunittest.
在一个单位测试组中,我请求良多纪录无效,我不想每次都写一样的设置代码,TSQLUnit利用了一个SetUp的标识设备:
Forinstance,thesetupfixtureforthepreviousMyTablesuitewouldbenamedut_MyTable_setup.Thethirdpartofthename"setup"alertsTSQLUnittotreattheprocedureasasetupfixtureforthesuite.Itwilllooksomethinglikethis:
比方,前缀为MyTable的组可使用ut_MyTable_Setup的设备:定名为Setup会让TSQLUnit以为这是个启动的设备.代码以下:
CREATEPROCEDUREut_MyTable_setup
AS
INSERTINTOMyTable([Description])
VALUES(something)
--(morerecordsinsertedhere
GO
TheSQLServercommunityowesahugedebtofgratitudetoHenrikEkelundandhisemployerformakingTSQLUnitopensource.
SQLServer社区极年夜的感谢HenrikEkelund和他的人员让TSQLUNnit开放源码.
Linktohttp://sourceforge.net/projects/tsqlunit
Linktohttp://tsqlunit.sourceforge.net/tsqlunit_cookbook.htm(documentation)
Archive非常适合存储大量的独立的,作为历史记录的数据。因为它们不经常被读取。Archive拥有高效的插入速度,但其对查询的支持相对较差 |
|