首页 > 开发 > 综合 > 正文

TSQLUnit简介(翻译)

2024-07-21 02:11:14
字体:
来源:转载
供稿:网友

introducing tsqlunit
tsqlunit简介
tsqlunit is an open source unit testing framework for t-sql written by henrik ekelund and available from http://sourceforge.net/projects/tsqlunit. here's an example of how i've used it.

my tsqlunit tests take a similar pattern of three parts:
1) unit test setup, 测试设置
2) execution of the target procedure, and 执行测试目标存储过程.
3) checking results.检查结果.

in the unit test setup, i often check to make sure someone hasn't done bad things to my data when i wasn't looking:
在单元测试的设置中我经常检查其他人没有破坏我希望的数据:

declare @nid int, @nnewid int —- @nnewid is for later
select @nid = [id] from mytable where myfield = 'whatever'
if @nid is null  -- or @@rowcount = 0
  exec tsu_failure 'the data has changed. ''whatever'' couldn''t be found'

the if block checks for the expected record. if it couldn't be found, the test fails and will generate an error message. the test framework moves on to the next unit test. you don't need to use the name of the unit test in the failure message string, because tsqlunit will name it for you when the test fails.
if块检查期望的记录.如果无法找到,测试失败并生成一个错误消息.测试框架移动到下一个单元测试.你不用在失败的消息中使用测试的名字,因为tsqlunit会在测试失败时命名的.
now i call the stored procedure i'm about to write:
现在我调用我要写的存储过程:
exec createmytablenewrec @nid, @nnewid output

as you can see, i've determined that i need an output parameter from this new procedure. in checking the results, i make sure the output parameter really is filled with something:
你看,我将检查存储过程中需要返回的参数.在检查结果中,要确认输出参数真的被填充了.
if @nnewid is null
  exec tsu_failure  'a new record was not created for table mytable.'

i could further check the value to see if the new record was created in the way i wanted it to be created.
我可以检查更深层次的值,如果我要求的新记录被创建了.
each tsqlunit test is itself a stored procedure. listing 1 shows what one looks like when all of the pieces are put together:
每个tsqlunit测试都是一个存储过程.列表1显示了所有测试段落在一起的情况.
listing 1. a complete unit test for t-sql.

create procedure ut_mytable_newrec
as
  --== setup ==--
  declare @nid int, @nnewid int
  select @nid = id from mytable
  where myfield = 'whatever'
  if @nid is null  -- or @@rowcount = 0
    exec tsu_failure 'the data has changed.
    ''whatever'' couldn''t be found'
  --== execute ==--
  exec createmytablenewrec @nid, @nnewid output
  --== check ==--
  if @nnewid is null
    exec tsu_failure 'a new record was not created
    for table mytable.'
go

note the three-part name of the stored procedure, ut_mytable_newrec. the prefix "ut_" alerts tsqlunit that this is a unit test it should run. if you already use this prefix ut_ for other purposes, tsqlunit lets you set it to something else. "mytable" is the name of a group of related unit tests, known as a suite of tests. for instance, you could add another unit test called ut_mytable_deleterec. the mytable suite would test both adding and deleting a record to mytable. the suite can be run separately from other test suites. the third part of the name–"newrec" or "deleterec"–uniquely identifies this unit test.
三个段落的存储过程,前缀ut_是告诉tsqlunit要运行的一个单元测试.如果你已经使用ut_前缀作其他的用途,tsqlunit要求你修改为其他的名字.名字的第二个段落,显示了测试的组,例如,你可以加入一个单元测试名字为ut_mytable_deleterec.这个组会测试添加和删除一个记录到mytable,组也可以分开到其他的测试组中.名字的第三个段落,是测试的唯一标示.
note that you don't need begin tran and rollback in each unit test; tsqlunit takes care of this for you.
你也不在需要begin tran和rollback在每个单元测试.tsqlunit负责为你处理

running the unit test
运行单元测试
in order to run the unit test in listing 1, you need to set up the framework. from query analyzer, run tsqlunit.sql on your development database. you need do this only once for the database. next, create procedure ut_mytable_newrec, if you haven't already. now you're set. simply execute the unit test:
为了运行列表1中的单元测试,你要设置测试框架.在查询分析器中,运行tsqlunit.sql在你的开发数据库中..你仅仅要执行一次在数据库中.创建ut_mytable_newrec,现在你可以简单的执行单元测试了:

-- this will run all tests for suite mytable, 这将运行mytable组的所有测试
exec tsu_runtests mytable

fixtures 设备
suppose i want numerous records to be available for all the unit tests of a suite. i don't want to write the same setup code for each test. tsqlunit solves the problem with a setup fixture. the code in the fixture will be run before each unit test.
在一个单元测试组中,我要求很多记录有效,我不想每次都写同样的设置代码,tsqlunit使用了一个setup的标识设备:

for instance, the setup fixture for the previous mytable suite would be named ut_mytable_setup. the third part of the name "setup" alerts tsqlunit to treat the procedure as a setup fixture for the suite. it will look something like this:
例如,前缀为mytable的组可以使用ut_mytable_setup的设备:命名为setup会让tsqlunit认为这是个启动的设备.代码如下:
create procedure ut_mytable_setup
as
  insert into mytable ([description])
  values ('something')
  --( more records inserted here
go

the sql server community owes a huge debt of gratitude to henrik ekelund and his employer for making tsqlunit open source.
sqlserver社区极大的感激henrik ekelund和他的职员让tsqlunnit开放源码.

link to http://sourceforge.net/projects/tsqlunit

link to http://tsqlunit.sourceforge.net/tsqlunit_cookbook.htm (documentation)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表