TSQLUnit簡介(翻譯)

來源:互聯網
上載者:User

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)



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。