本主題示範如何使用單元測試功能,在 FlatFileReceive 管線範例中新增管線的單元測試。 管線單元測試類似於這裡記載的 Pipeline.exe 工具: 管線工具。 當您在專案屬性的 [ 部署 ] 索引卷標上啟用單元測試時,專案中的管線類別衍生自 Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline。 這個類別會建立 Pipeline.exe 工具所公開的一些相同功能模型。
先決條件
您必須先遵循建置 FlatFileReceive 範例的步驟,並熟悉該範例。 您可以在這裡找到包含建置 FlatFileReceive 範例的步驟的檔:FlatFileReceive (BizTalk Server 範例)。
將單元測試專案新增至 FlatFileReceive 範例
將單元測試專案新增至 FlatFileReceive 範例
在 Visual Studio 中,開啟FlatFileReceive.sln方案檔。
在 [方案總管] 中,以滑鼠右鍵按兩下 FlatFileReceive 項目,然後按兩下 [ 屬性]。
在 [項目設計工具] 中,按下 [ 部署 ] 屬性頁索引標籤,並將 [ 啟用單元測試 ] 設定為
True。關閉儲存變更的項目屬性頁面。
在主功能表上,按兩下 [ 建置],然後按兩下 [ 重建方案]。
在主功能表上,按兩下 [ 測試],然後按兩下[ 新增測試]。
在 [新增測試] 對話框中,選取 [新增至測試專案] 字段的 [建立新的 Visual C# 測試專案]。 在 [範本] 清單中選取 [單元測試精靈],然後按兩下 [確定]。
在 [ 新增測試專案 ] 對話框中,將專案名稱保留為 TestProject1 ,然後按兩下 [ 建立]。
在 [建立單元測試] 對話框中,展開類型,然後選取 Microsoft.Samples.BizTalk.FlatFileReceive.FFReceivePipeline 節點下的 FFReceivePipeline() 建構函式。 按一下 [確定]。
新增測試程式代碼以測試管線
若要新增測試程式碼以測試管道
將下列參考新增至 TestProject1 專案:
BizTalk 管道 Interop
Microsoft.BizTalk.TestTools
Microsoft XLANG/s 基底類型
在 [方案總管] 中,開啟 FFReceivePipelineTest.cs,並在該檔案頂端加入以下指令:
using System.IO; using System.Collections.Specialized; using System.Collections.Generic;捲動至檔案底部,並將 FFReceivePipelineConstructorTest 方法取代為下列程式代碼,以驗證管線輸入是否存在,再測試管線。 此程式代碼也會驗證已產生符合一般檔案架構的訊息。
[TestMethod()] public void FFReceivePipelineUnitTest() { //=== Pipeline class derived from TestableReceivePipeline ===// FFReceivePipeline target = new FFReceivePipeline(); //=== Collection of messages to test the flat file pipeline ===// StringCollection documents = new StringCollection(); string strSourcePO_XML = @".\..\..\..\FlatFileReceive_in.txt"; Assert.IsTrue(File.Exists(strSourcePO_XML)); documents.Add(strSourcePO_XML); //=== Only a body part for this test message so an empty ===// //=== collection will be passed. ===// StringCollection parts = new StringCollection(); //=== Dictionary mapping the schema to the namespace and type ===// //=== as displayed in the properties window for the *.xsd ===// Dictionary<string, string> schemas = new Dictionary<string, string>(); string SchemaFile = @".\..\..\..\PO.xsd"; Assert.IsTrue(File.Exists(SchemaFile)); schemas.Add("Microsoft.Samples.BizTalk.FlatFileReceive.PO", SchemaFile); //=== Test the execution of the pipeline using the inputs ===// target.TestPipeline(documents, parts, schemas); //=== Validate that the pipeline test produced the message ===// //=== which conforms to the schema. ===// string[] strMessages = Directory.GetFiles(testContextInstance.TestDir + "\\out","Message*.out"); Assert.IsTrue(strMessages.Length > 0); PO PO_target = new PO(); foreach(string outFile in strMessages) { Assert.IsTrue(PO_target.ValidateInstance(outFile,Microsoft.BizTalk.TestTools.Schema.OutputInstanceType.XML)); } }
建置和執行單元測試
建置和執行單元測試
在 [方案總管] 中,以滑鼠右鍵點選 TestProject1,然後點選 建置。
在主功能表上,按兩下 [ 測試],然後在 [Windows ] 清單中,按兩下 [ 測試檢視]。
在 [測試檢視] 視窗中,以滑鼠右鍵按一下 FFReceivePipelineUnitTest,然後按一下 執行已選項目。 確認您在 [測試結果] 視窗中看到 [通過 ]。
在 TestResults 目錄中,檢查 *.out 檔案。 此檔案應該包含管線所處理的新訊息。 它應該位於類似下列的目錄中:
C:\Program Files\Microsoft BizTalk Server <version>\SDK\Samples\Pipelines\AssemblerDisassembler\FlatFileReceive\TestResults\Wes_BTS2009Svr 2009-02-04 09_01_04\Out
已處理的訊息看起來應該如下所示:
<purchaseOrder orderDate="1999-10-20" xmlns="http://FlatFileReceive.PO"> <shipTo country="US" xmlns=""> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US" xmlns=""> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items xmlns=""> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment xmlns="http://FlatFileReceive.PO">Confirm this is electric</comment> </item> <item partNum="926-AA"> <productName>Baby Monitor</productName> <quantity>1</quantity> <USPrice>39.98</USPrice> <comment xmlns="http://FlatFileReceive.PO">Confirm this is electric</comment> <shipDate>1999-05-21</shipDate> </item> </items> </purchaseOrder>如果有任何測試失敗,您可以在 [測試結果] 視窗中按兩下測試,以查看造成該測試失敗的判斷提示或例外狀況。
測試程式代碼摘要
針對 FlatFileReceive 專案啟用單元測試時,與 FFReceivePipeline C# 類別相關聯的 FFReceivePipeline.btp 衍生自 Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline 類別。 TestProject1 中的 FFReceivePipelineUnitTest 方法使用 FFReceivePipeline 繼承的 TestPipeline 方法來測試一般檔案接收管線。 管線處理訊息之後,輸出訊息會根據一般檔案架構進行驗證。 TestPipeline 方法的參數如下所示:
| 參數名稱 | 說明 |
|---|---|
| 文件 | StringCollection 包含要由管線處理的訊息。 |
| 組件 | StringCollection 包含訊息的元件。 |
| 模式 | 字典映射用來將每個訊息類型對應到其相應的 *.xsd 結構檔案。 索引鍵的格式必須是 Namespace.Type。 應從 Visual Studio 中 *.xsd 檔案的屬性視窗指出所使用的命名空間和類型。 請參閱下面的螢幕擷取畫面。
XSD 檔案屬性視窗中所公開的命名空間與類型。 |