分享
 
 
 

TSQLUnit简介(翻译)

王朝mssql·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有