将单元测试功能与管道配合使用

本主题演示如何使用单元测试功能在 FlatFileReceive 管道示例中为管道添加单元测试。 管道单元测试类似于此处所述的 Pipeline.exe 工具: 管道工具。 在项目属性的 “部署 ”选项卡上启用单元测试时,项目中的管道类派生自 Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline。 此类模拟了 Pipeline.exe 工具所公开的某些功能。

先决条件

必须首先遵循生成 FlatFileReceive 示例并熟悉该示例的步骤。 此处提供了包含生成 FlatFileReceive 示例的步骤的文档:FlatFileReceive (BizTalk Server 示例)。

将单元测试项目添加到 FlatFileReceive 示例

将单元测试项目添加到 FlatFileReceive 示例

  1. 在 Visual Studio 中,打开FlatFileReceive.sln解决方案文件。

  2. 在解决方案资源管理器中,右键单击 FlatFileReceive 项目,然后单击“ 属性”。

  3. 在项目设计器中,单击 “部署 属性”页选项卡,并将 “启用单元测试 ”设置为 True

  4. 关闭保存更改的项目属性页。

  5. 在主菜单上,单击“ 生成”,然后单击“ 重新生成解决方案”。

  6. 在主菜单上,单击“ 测试”,然后单击“ 新建测试”。

  7. 在“添加新测试”对话框中,为“添加到测试项目”字段选择“创建新的 Visual C# 测试项目”。 在“模板”列表中选择“单元测试向导”,然后单击“确定”。

  8. 在“ 新建测试项目 ”对话框中,将项目名称保留为 TestProject1 ,然后单击“ 创建”。

  9. 在“创建单元测试”对话框中,展开类型并选择 Microsoft.Samples.BizTalk.FlatFileReceive.FFReceivePipeline 节点下的 FFReceivePipeline() 构造函数。 单击 “确定”

添加测试代码以测试管道

添加测试代码以测试管道

  1. 将以下引用添加到 TestProject1 项目:

    • BizTalk 管道互操作

    • Microsoft.BizTalk.TestTools

    • Microsoft XLANG/s 基类型

  2. 在解决方案资源管理器中,打开FFReceivePipelineTest.cs,并将以下指令添加到该文件顶部:

    using System.IO;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    
  3. 滚动到文件的底部,并将 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));
        }
    }
    

生成并运行单元测试

生成并运行单元测试

  1. 在解决方案资源管理器中,右键单击 TestProject1,然后单击“ 生成”。

  2. 在主菜单上,单击“ 测试”,然后在 Windows 列表中单击“ 测试视图”。

  3. 在“测试视图”窗口中,右键单击 FFReceivePipelineUnitTest,然后单击“ 运行选择”。 验证你是否在“测试结果”窗口中看到通过

  4. 在 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>
    
  5. 如果任何测试失败,可以在“测试结果”窗口中双击测试,以查看导致该测试失败的断言或异常。

测试代码摘要

FlatFileReceive 项目启用单元测试后,与 FFReceivePipeline.btp 关联的 FFReceivePipeline C# 类派生自 Microsoft.BizTalk.TestTools.Pipeline.TestableReceivePipeline 类。 TestProject1 中的 FFReceivePipelineUnitTest 方法使用 FFReceivePipeline 继承的 TestPipeline 方法来测试平面文件接收管道。 管道处理消息后,根据平面文件架构验证输出消息。 TestPipeline 方法的参数如下所示:

参数名称 DESCRIPTION
文档 包含将由管道处理的消息的 StringCollection 集合。
部件 包含消息部分的 StringCollection。
模式 用于将每个消息类型映射到其相应的 *.xsd 架构文件的字典映射。 密钥必须采用 Namespace.Type 格式。 应从 Visual Studio 中 *.xsd 文件的属性窗口中记录使用的命名空间和类型。 请参阅以下屏幕截图。

突出显示所选 XSD 文件的命名空间和类型的图像。

从 XSD 文件的属性窗口公开的命名空间和类型。

另请参阅

在架构和映射中使用单元测试功能与单元测试协作(Visual Studio)