单元测试 TelemetryProcessor 和 TelemetryInitializer(Application Insights)?
Unit Testing TelemetryProcessor and TelemetryInitializer (Application Insights)?
我一直在网上寻找对我的处理器和初始化器进行单元测试的方法。到目前为止,我已经从另一篇文章中设置了以下代码,但我对如何继续操作有点迷茫。
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();
}
}
我不确定应该在哪里添加 telemtryprocessor,以便 Application Inisight 知道我正在对其进行单元测试。如果有人知道怎么做,将不胜感激。
这是我们对遥测初始化程序进行单元测试的服务之一的示例。
首先,我们有 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);
}
我一直在网上寻找对我的处理器和初始化器进行单元测试的方法。到目前为止,我已经从另一篇文章中设置了以下代码,但我对如何继续操作有点迷茫。
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();
}
}
我不确定应该在哪里添加 telemtryprocessor,以便 Application Inisight 知道我正在对其进行单元测试。如果有人知道怎么做,将不胜感激。
这是我们对遥测初始化程序进行单元测试的服务之一的示例。
首先,我们有 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);
}