Skip to content

[Test Improver] Add unit tests for LoggingManager.BuildAsync #7995

@Evangelink

Description

@Evangelink

🤖 Test Improver — automated AI assistant focused on improving tests for this repository.

Goal & Rationale

LoggingManager is the central class responsible for building the ILoggerFactory used throughout Microsoft.Testing.Platform. Despite its critical role in the logging pipeline, it had zero unit tests. This PR adds 9 focused tests covering all meaningful branches.

Approach

The tests exercise LoggingManager directly (it's internal, accessible via InternalsVisibleTo). Mocks are created with Moq for the external dependencies (IMonitor, IServiceProvider, ILoggerProvider, IExtension, IAsyncInitializableExtension). Multi-interface provider types are expressed as internal interface combinations so Moq can create a single mock implementing all required interfaces.

Tests added (LoggingManagerTests.cs)

Test What it covers
AddProvider_NullFactory_ThrowsArgumentNullException Null guard on AddProvider
BuildAsync_NoProviders_ReturnsNonNullFactory Empty factory created when no providers are registered
BuildAsync_PassesCorrectLogLevelAndServiceProviderToFactory Factory delegate receives the LogLevel and IServiceProvider passed to BuildAsync
BuildAsync_NonExtensionProvider_IsAlwaysIncluded Plain ILoggerProvider (not IExtension) is always added to the factory
BuildAsync_EnabledExtensionProvider_IsIncluded IExtension.IsEnabledAsync() == true → provider included
BuildAsync_DisabledExtensionProvider_IsExcluded IExtension.IsEnabledAsync() == false → provider skipped
BuildAsync_InitializableExtensionProvider_WhenEnabled_CallsInitializeAsync Enabled IAsyncInitializableExtension has InitializeAsync called
BuildAsync_InitializableExtensionProvider_WhenDisabled_DoesNotCallInitializeAsync Disabled IAsyncInitializableExtensionInitializeAsync never called
BuildAsync_NonExtensionInitializableProvider_CallsInitializeAsync Non-extension IAsyncInitializableExtension (no IsEnabledAsync guard) → InitializeAsync always called

Coverage Impact

TFM Before After
net8.0 ~659 tests ~668 tests (+9)
net9.0 ~659 tests ~668 tests (+9)
Total ~1318 ~1336 (+18)

Test Status

✅ All 18 tests pass (9 per TFM):

dotnet build test/UnitTests/Microsoft.Testing.Platform.UnitTests/Microsoft.Testing.Platform.UnitTests.csproj -c Debug
dotnet test test/UnitTests/Microsoft.Testing.Platform.UnitTests/Microsoft.Testing.Platform.UnitTests.csproj -c Debug --filter "LoggingManager"
# total: 18 | passed: 18 | failed: 0

Trade-offs

  • Uses Moq (already a dependency in this test project) for concise mocks
  • Three internal interface helpers defined at the bottom of the test file for multi-interface mocks — lightweight and keep the test code clean
  • Tests directly instantiate LoggingManager (internal), avoiding integration overhead

Generated by Daily Test Improver · ● 2.4M ·


Note

This was originally intended as a pull request, but the git push operation failed.

Workflow Run: View run details and download patch artifact

The patch file is available in the agent artifact in the workflow run linked above.

To create a pull request with the changes:

# Download the artifact from the workflow run
gh run download 25265145801 -n agent -D /tmp/agent-25265145801

# Create a new branch
git checkout -b test-assist/logging-manager-buildAsync-tests-7351033ae996670b

# Apply the patch (--3way handles cross-repo patches where files may already exist)
git am --3way /tmp/agent-25265145801/aw-test-assist-logging-manager-buildasync-tests.patch

# Push the branch to origin
git push origin test-assist/logging-manager-buildAsync-tests-7351033ae996670b

# Create the pull request
gh pr create --title '[Test Improver] Add unit tests for LoggingManager.BuildAsync' --base main --head test-assist/logging-manager-buildAsync-tests-7351033ae996670b --repo microsoft/testfx
Show patch preview (180 of 180 lines)
From ae56d09e1a8f74bdee885913817aee6b14f3a5b5 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Sun, 3 May 2026 00:10:15 +0000
Subject: [PATCH] test: add unit tests for LoggingManager.BuildAsync

Cover all key behaviors of LoggingManager:
- AddProvider with null throws ArgumentNullException
- BuildAsync with no providers returns a non-null factory
- Factory delegate receives the correct LogLevel and IServiceProvider
- Non-IExtension provider is always included
- IExtension provider enabled -> included; disabled -> excluded
- IAsyncInitializableExtension + enabled -> InitializeAsync called
- IAsyncInitializableExtension + disabled -> InitializeAsync not called
- Non-extension IAsyncInitializableExtension -> InitializeAsync called

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 .../Logging/LoggingManagerTests.cs            | 149 ++++++++++++++++++
 1 file changed, 149 insertions(+)
 create mode 100644 test/UnitTests/Microsoft.Testing.Platform.UnitTests/Logging/LoggingManagerTests.cs

diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Logging/LoggingManagerTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Logging/LoggingManagerTests.cs
new file mode 100644
index 0000000..cab4910
--- /dev/null
+++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Logging/LoggingManagerTests.cs
@@ -0,0 +1,149 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+using Microsoft.Testing.Platform.Extensions;
+using Microsoft.Testing.Platform.Helpers;
+using Microsoft.Testing.Platform.Logging;
+
+using Moq;
+
+namespace Microsoft.Testing.Platform.UnitTests;
+
+[TestClass]
+public sealed class LoggingManagerTests
+{
+    private readonly Mock<IMonitor> _mockMonitor = new();
+    private readonly Mock<IServiceProvider> _mockServiceProvider = new();
+
+    public Lo
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions