搭配管線使用單元測試功能
此主題示範如何使用單元測試功能,以在 FlatFileReceive 管線範例中新增管線的單元測試。 管線單元測試類似于此處記載的 Pipeline.exe 工具: 管線工具。 當您在專案屬性的 [部署 ] 索引標籤上啟用單元測試時,專案中的管線類別衍生自 Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline。 此類別可做為 Pipeline.exe 工具所公開之一些相同功能的模型。
必要條件
您必須先遵循建置 FlatFileReceive 範例的步驟進行,然後便可熟悉該範例。 您可以在這裡找到包含建置 FlatFileReceive 範例的步驟檔:FlatFileReceive (BizTalk Server Sample) 。
將單元測試專案新增至 FlatFileReceive 範例
將單元測試專案新增至 FlatFileReceive 範例
在 Visual Studio 中,開啟 FlatFileReceive.sln 方案檔。
在方案總管中,以滑鼠右鍵按一下FlatFileReceive專案,然後按一下 [屬性]。
在 [專案Designer] 中,按一下 [部署] 屬性頁索引標籤,然後將[啟用單元測試]設定為
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.btp 相關聯的 FFReceivePipeline.btp衍生自Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline類別。 TestProject1中的FFReceivePipelineUnitTest方法使用FFReceivePipeline繼承的TestPipeline方法來測試一般檔案接收管線。 在管線處理訊息之後,就會針對一般檔案結構描述來驗證輸出訊息。 TestPipeline方法的參數如下所示:
參數名稱 | 描述 |
---|---|
文件 | StringCollection 包含要由管線處理的訊息。 |
組件 | StringCollection 包含訊息的數個部分。 |
結構描述 | 目錄對應,用來將每個訊息類型對應至其對應的 *.xsd 結構描述檔案。 索引鍵的格式必須是 Namespace.Type。 使用的命名空間和類型應該從 Visual Studio 中 *.xsd 檔案的屬性視窗注意。 請參閱下面的螢幕擷取畫面。![]() 從 XSD 檔案的屬性視窗中公開的命名空間與類型。 |