用于创建诊断数据适配器的示例项目

“MyDiagnosticDataAdapter”是可以将日志文件附加到测试结果的一个简单的诊断数据适配器,则在运行测试时。

您需要在安装了诊断数据收集器或配置编辑器的任何计算机上的管理权限。

示例

本示例演示如何执行以下任务:

  • 应用特性以使类可检测到于 Microsoft 测试管理器 作为诊断数据适配器。

  • 应用特性以使用户控件类可检测到于 Microsoft 测试管理器 用作编辑使用更改诊断数据适配器的配置。

  • 访问默认配置数据。

  • 注册特定诊断数据收集事件。

  • 通过发送它将日志文件附加到 DataCollectionSink

// My Data Collector Class
using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Execution;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System;

namespace MyCompany.MyDiagnosticDataAdapters
{
    // Provide a URI and friendly name for your diagnostic data adapter
    [DataCollectorTypeUri("datacollector://MyCompany/MyDataCollector/1.0")]
    [DataCollectorFriendlyName("Collect Log Files sample", false)]
    // Designate your chosen configuration editor
    [DataCollectorConfigurationEditor(
        "configurationeditor://MyCompany/MyDataConfigEditor/1.0")]
    public class MyDataCollector : DataCollector
    {
        private DataCollectionEvents dataEvents;
        private DataCollectionLogger dataLogger;
        private DataCollectionSink dataSink;
        private XmlElement configurationSettings;

        // Required method called by the testing framework
        public override void Initialize(
            XmlElement configurationElement, 
            DataCollectionEvents events, 
            DataCollectionSink sink, 
            DataCollectionLogger logger, 
            DataCollectionEnvironmentContext environmentContext)
        {
            dataEvents = events; // The test events
            dataLogger = logger; // The error and warning log
            dataSink = sink;     // Saves collected data
            // Configuration from the test settings
            configurationSettings = configurationElement;

            // Register common events for the data collector
            // Not all of the events are used in this class
            dataEvents.SessionStart +=
                new EventHandler<SessionStartEventArgs>(OnSessionStart);
            dataEvents.SessionEnd +=
                new EventHandler<SessionEndEventArgs>(OnSessionEnd);
            dataEvents.TestCaseStart +=
                new EventHandler<TestCaseStartEventArgs>(OnTestCaseStart);
            dataEvents.TestCaseEnd +=
                new EventHandler<TestCaseEndEventArgs>(OnTestCaseEnd);
            dataEvents.DataRequest +=
                new EventHandler<DataRequestEventArgs>(OnDataRequest);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                dataEvents.SessionStart -=
                    new EventHandler<SessionStartEventArgs>(OnSessionStart);
                dataEvents.SessionEnd -=
                    new EventHandler<SessionEndEventArgs>(OnSessionEnd);
                dataEvents.TestCaseStart -=
                    new EventHandler<TestCaseStartEventArgs>(OnTestCaseStart);
                dataEvents.TestCaseEnd -=
                    new EventHandler<TestCaseEndEventArgs>(OnTestCaseEnd);
                dataEvents.DataRequest -=
                    new EventHandler<DataRequestEventArgs>(OnDataRequest);
            }
        }

        #region Event Handlers
        public void OnSessionStart(object sender, SessionStartEventArgs e)
        {
            // TODO: Provide implementation
        }

        public void OnSessionEnd(object sender, SessionEndEventArgs e)
        {
            // TODO: Provide implementation
        }

        public void OnTestCaseStart(object sender, TestCaseEventArgs e)
        {
            // TODO: Provide implementation
        }

        public void OnTestCaseEnd(object sender, TestCaseEndEventArgs e)
        {
            // Get any files to be collected that are
            // configured in your test settings
            List<string> files = getFilesToCollect();

            // For each of the files, send the file to the data sink
            // which will attach it to the test results or to a bug
            foreach (string file in files)
            {
                dataSink.SendFileAsync(e.Context, file, false);
            }
        }

        public void OnDataRequest(object sender, DataRequestEventArgs e)
        {
            // TODO: Provide implementation
            // Most likely this occurs because a bug is being filed
        }
        #endregion

        // A private method to collect the configured file names
        private List<string> getFilesToCollect()
        {
            // Seetup namespace manager with our namespace
            XmlNamespaceManager nsmgr =
                new XmlNamespaceManager(
                    configurationSettings.OwnerDocument.NameTable);
            nsmgr.AddNamespace("ns", 
                "http://MyCompany/schemas/MyDataCollector/1.0");

            // Find all of the "File" elements under our configuration
            XmlNodeList files =
                configurationSettings.SelectNodes(
                    "//ns:MyDataCollector/ns:File");
            
            // Build the list of files to collect from the 
            // "FullPath" attributes of the "File" nodes.
            List<string> result = new List<string>();
            foreach (XmlNode fileNode in files)
            {
                XmlAttribute pathAttribute = 
                    fileNode.Attributes["FullPath"];
                if (pathAttribute != null &&
                    !String.IsNullOrEmpty(pathAttribute.Value))
                {
                    result.Add(pathAttribute.Value);
                }
            }

            return result;
        }
    }
}

这是诊断数据适配器的示例配置编辑器。将用户控件添加到您的项目并创建具有标签的简单形式 (“日志文件的名称: ") 和名为 “FileTextBox”的文本框。

using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Execution;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System;

namespace MyCompany.DiagnosticDataAdapters.ConfigurationEditors
{
    [DataCollectorConfigurationEditorTypeUri(
        "configurationeditor://MyCompany/MyConfigEditor/1.0")]
    public partial class MyDataConfigEditor :
        UserControl, IDataCollectorConfigurationEditor
    {
        private DataCollectorSettings collectorSettings;

        // Create a private property for the service provider
        private IServiceProvider ServiceProvider { get; set; }

        // Constructor
        public MyConfigurationEditor()
        {
            InitializeComponent();
        }

        // Required method called by the testing framework
        public void Initialize(
            IServiceProvider svcProvider,
            DataCollectorSettings settings)
        {
            ServiceProvider = svcProvider;
            collectorSettings = settings;

            // Display the file name as listed in the settings file.
            // If the configuration has not been updated before, this
            // data will be provided by the default configuration
            // section from <nameofcollector>.dll.config:
            // <DefaultConfiguration>
            //   <MyCollectorName 
            //       xmlns="http://MyCompany/schemas/ProductName/Version");
            //     <File FullPath="C:\temp\logfile1.txt" />
            //   </MyCollectorName>
            // </DefaultConfiguration>
            this.SuspendLayout();
            this.FileTextBox.Text = GetText(collectorSettings.Configuration);
            this.ResumeLayout();
        }

        // Can be used to verify data before saving it
        public bool VerifyData()
        {
            // Not currently verifying data
            return true;
        }

        // Reset to default value from XML configuration
        // using a custom method
        public void ResetToAgentDefaults()
        {
            this.FileTextBox.Text = 
                getText(collectorSettings.DefaultConfiguration);
        }

        // Saves data changed in the editor to the test configuration
        // settings. Does not change the default value.
        public DataCollectorSettings SaveData()
        {
            collectorSettings.Configuration.InnerXml =
                String.Format(
                    @"<MyCollectorName
      http://MyCompany/schemas/MyDataCollector/1.0"">
  <File FullPath=""{0}"" />
</MyCollectorName>",
                    FileTextBox.Text);
            return collectorSettings;
        }

        // Reads the configuration settings
        private string getText(XmlElement element)
        {
            // Setup namespace manager with our namespace
            XmlNamespaceManager nsmgr =
                new XmlNamespaceManager(
                    element.OwnerDocument.NameTable);

            // Find all the "File" elements under our configuration
            XmlNodeList files = element.SelectNodes("//ns:MyDataCollector/ns:File", nsmgr);
            
            string result = String.Empty;
            if (files.Count > 0)
            {
                XmlAttribute pathAttribute = files[0].Attributes["FullPath"];
                if (pathAttribute != null &&
                    !String.IsNullOrEmpty(pathAttribute.Value))
                {
                    result = pathAttribute.Value;
                }
            }

            return result;
        }
    }
}

下面是诊断数据收集器配置编辑器的示例配置文件。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section 
      name="DataCollectorConfiguration"
      type="Microsoft.VisualStudio.QualityTools.Execution.DataCollectorConfigurationSection,
        Microsoft.visualStudio.QualityTools.ExecutionCommon,
        Version=4.0.0.0, Culture=neutral,
        PublicKeyToken=b03f5f7f11d50a3a" />
  </configSections>
  <DataCollectorConfiguration
      xmlns="https://microsoft.com/schemas/VisualStudio/TeamTest/11">
    <DataCollector 
        typeUri="datacollector://MyCompany/MyDataCollector/1.0">
      <DefaultConfiguraton>
        <!-- Your default config settings-->
        <File FullPath="c:\temp\logfile1.txt" />
      </DefaultConfiguration>
    </DataCollector>
  </DataCollectorConfiguration>
</configuration>

编译代码

若要创建此诊断适配器的代码项目

  1. 创建名为 “MyDataCollector”的新类库项目。

  2. 解决方案资源管理器,右击该项目并选择 属性。若要选择添加的文件夹,选择 引用路径 然后选择省略号 ()。

    选择引用路径 显示对话框。

  3. 基于您的安装目录选择以下路径,例如: Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies。选择 确定

  4. 若要将文件夹添加到您的引用路径,选择 添加文件夹

    该文件夹将显示在引用路径列表中。

  5. 选择 全部保存 图标保存引用路径。

  6. 添加程序集 Microsoft.VisualStudio.QualityTools.ExecutionCommon

    1. 解决方案资源管理器,右击 引用 然后选择 添加引用

    2. 选择 浏览 并找到 Microsoft.VisualStudio.QualityTools.ExecutionCommon.dll

      此程序集。 Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies中找到。

    3. 选择 确定

  7. 添加程序集 Microsoft.VisualStudio.QualityTools.Common

    1. 在解决方案资源管理器中,右击 引用 并选择 添加引用

    2. 选择 浏览 并找到 Microsoft.VisualStudio.QualityTools.Common.dll

      此程序集。 Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies中找到。

    3. 选择 确定

  8. 复制在本文档前面列出到您的类库类的诊断数据适配器类。保存此类。

  9. 若要将用户控件添加到该项目中,右击在解决方案资源管理器中 MyDataCollector 项目,指向 add,然后选择 用户控件。选择 add

  10. 使用工具箱添加一个标签,到用户控件并更改文本属性更改为文件名:。

  11. 使用工具箱,添加文本框添加到用户控件并将名称更改为 " textBoxFileName "。

  12. 解决方案资源管理器,右击用户控件然后选择 查看代码。 在本文档前面列出的用户控件类来替换此用户控件类。保存此类。

    说明说明

    默认情况下,用户控件名为 UserControl1。确保该用户控件类代码使用您的用户控件的名称。

  13. 若要创建配置文件,在 解决方案资源管理器 右击解决方案,指向 add,然后选择 新建项。选择 应用程序配置文件,然后选择 add。这将添加一个名为 " 解决方案的 App.config 的文件。

  14. 复制提供前到 XML 文件的示例的 XML。保存文件。

  15. 生成解决方案,然后复制生成的程序集和 App.config 文件添加到 Program Files\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\DataCollectors 内容。

  16. 创建测试使用此自定义诊断数据适配器的设置。将测试设置配置为收集存在的文件。

    如果您是从运行测试时从 Microsoft 测试管理器测试,则可以将这些测试设置分配给测试计划,在运行测试前或请使用运行带有选项命令分配测试设置,并重写测试设置。有关测试设置的更多信息,请参见 使用测试设置来设置计算机和收集诊断信息

  17. 运行测试使用选择了您的诊断数据适配器的测试设置。

    您指定的数据文件将被附加到测试结果中,执行测试时。

请参见

概念

如何:创建诊断数据适配器

如何:为诊断数据适配器创建自定义数据编辑器

如何:安装自定义诊断数据适配器

创建诊断数据适配器以收集自定义数据或影响测试计算机