【问题标题】:Unit Testing TelemetryProcessor and TelemetryInitializer (Application Insights)?单元测试 TelemetryProcessor 和 TelemetryInitializer (Application Insights)?
【发布时间】:2021-09-18 08:35:03
【问题描述】:

我一直在网上寻找一种方法来对我的处理器和初始化程序进行单元测试。到目前为止,我从另一篇文章中获得了以下代码设置,但我对如何进行有点迷茫。

public class TelemetryClientTests
{
    private TelemetryClient telemetryClient;
    private List<ITelemetry> sendItems;

    [TestInitialize]
    public void TestInit()
    {
        var httpContext = Substitute.For<IHttpContextAccessor>();
        var configuration = new TelemetryConfiguration();
        //this.sendItems = new List<ITelemetry>();
        configuration.TelemetryChannel = new RequeteTelemtry();
        configuration.InstrumentationKey = Guid.NewGuid().ToString();
        configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
        this.telemetryClient = new TelemetryClient(configuration);
    }

    /// <summary>
    /// Ensure that context being propagated via async/await.
    /// </summary>
    [TestMethod]
    public void ContextPropogatesThruAsyncAwait()
    {
        var task = this.TestAsync();
        task.Wait();
    }
}

我不确定应该在哪里添加远程处理器,以便 Application Inisight 知道我正在对其进行单元测试。如果有人知道怎么做,将不胜感激。

【问题讨论】:

    标签: unit-testing azure-application-insights open-telemetry


    【解决方案1】:

    这是我们对遥测初始化程序进行单元测试的服务之一的示例。

    首先,我们有NullTelemetryProcessor。它有两个作用:防止数据实际发送到 Application Insights 服务;它将所有项目存储在内存中(因此可以将它们用于断言)。

    注意,遥测处理器在遥测初始化器之后运行。

    public class NullTelemetryProcessor : ITelemetryProcessor
    {
        public List<ITelemetry> Items = new List<ITelemetry>();
    
        public void Process(ITelemetry item)
        {
            this.Items.Add(item);
        }
    }
    

    然后我们注册它:

    [TestInitialize]
    public void Initialize()
    {
        // Initialization of a telemetry configuration
        this.telemetryConfiguration = new TelemetryConfiguration();
    
        // Adding a telemetry processor which prevents actually sending anything
        this.nullTelemetryProcessor = new NullTelemetryProcessor();
        TelemetryProcessorChainBuilder builder = this.telemetryConfiguration.TelemetryProcessorChainBuilder;
        builder.Use(next => this.nullTelemetryProcessor);
        builder.Build();
    }
    

    那么测试可能如下所示:

    [TestMethod]
    public void AppInsightsTelemetryClient_TrackException()
    {
        // Act
        this.telemetryClient.TrackException(new Exception("myexception"));
        this.telemetryClient.Flush();
    
        // Assert
        Assert.AreEqual(1, this.nullTelemetryProcessor.Items.Count);
        ExceptionTelemetry telemetryItem = this.nullTelemetryProcessor.Items[0] as ExceptionTelemetry;
        Assert.IsNotNull(telemetryItem);
        Assert.IsNull(telemetryItem.Context?.Operation?.ParentId);
        Assert.IsNull(telemetryItem.Context?.Operation?.Id);
    }
    

    【讨论】:

    • 好代码!!我的处理器负责过滤掉查询字符串中的敏感数据。在 TestMethod 中进行单元测试的合适方法是什么?
    • 我找到了方法:)!!再次感谢你的帮助。我赞成你的回答:)!!
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多