Skip to content

[test-improver] Add unit tests for DisposeHelper #10646

Closed

Description

@github-actions

Adds unit tests for DisposeHelper (src/Platform/Microsoft.Testing.Platform/Helpers/DisposeHelper.cs), an internal helper used by core host disposal paths (CommonTestHost, TestHostControllersTestHost, TestApplication, etc.) that previously had zero test coverage.

Goal and rationale

DisposeHelper.DisposeAsync has non-trivial branching logic: it conditionally invokes IAsyncCleanableExtension.CleanupAsync, then prefers IAsyncDisposable.DisposeAsync over IDisposable.Dispose on NETCOREAPP targets, while falling back to synchronous Dispose only on non-NETCOREAPP targets. A regression here (e.g. calling both DisposeAsync and Dispose, or skipping cleanup) would silently leak resources or double-dispose objects during host teardown.

Approach

Added DisposeHelperTests.cs with test-double classes exercising each combination:

  • null input (no-op)
  • object implementing only IAsyncCleanableExtension
  • object implementing both IAsyncDisposable + IDisposable (net8.0/net9.0 only — verifies DisposeAsync is called and Dispose is not)
  • object implementing only IDisposable (separate assertions for NETCOREAPP vs. netstandard/net462 builds, since the sync fallback path differs)
  • object implementing both IAsyncCleanableExtension and IDisposable (verifies both are invoked)
  • plain object implementing neither (no-op, no throw)

Since DisposeHelper carries [Embedded] (compiled as linked source into consumer projects rather than shipped in the main DLL), it wasn't visible to the test project via InternalsVisibleTo alone. Added a <Compile Include> link for DisposeHelper.cs in the test .csproj, following the same pattern already used for other [Embedded] helpers (TimeoutHelper.cs, ApplicationStateGuard.cs, etc.) in that project.

Test status

  • dotnet build ... -f net8.0 and -f net9.0: succeeded, 0 errors/warnings.
  • Full Microsoft.Testing.Platform.UnitTests suite (net8.0): 2213 total, 2194 succeeded, 0 failed, 19 skipped (pre-existing Windows-only skips).
  • New tests only (--filter "FullyQualifiedName~DisposeHelperTests"): 6/6 passed.
  • dotnet format whitespace --verify-no-changes: clean.

Reproduction

./build.sh -restore
dotnet build test/UnitTests/Microsoft.Testing.Platform.UnitTests -f net8.0
./artifacts/bin/Microsoft.Testing.Platform.UnitTests/Debug/net8.0/Microsoft.Testing.Platform.UnitTests --filter "FullyQualifiedName~DisposeHelperTests"

Trade-offs

Small, low-maintenance addition — test doubles are minimal and mirror the interfaces under test directly, so they should stay in sync with DisposeHelper's logic without much upkeep.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Test Improver workflow. · auto · 233.1 AIC · ⌖ 17.6 AIC · ⊞ 19.4K · [◷]( · ◷)
Comment /test-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@main

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch test-assist/dispose-helper-tests-45cab52a22d5926b.

Click here to create the pull request

To fix the permissions issue, go to Settings → Actions → General and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (157 of 157 lines)
From 22a3d10628ec3faaf21c4ee4f8e0e07980a91f37 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Tue, 18 Aug 2026 23:15:13 +0000
Subject: [PATCH] Add unit tests for DisposeHelper

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 .../Helpers/DisposeHelperTests.cs             | 123 ++++++++++++++++++
 ...icrosoft.Testing.Platform.UnitTests.csproj |   1 +
 2 files changed, 124 insertions(+)
 create mode 100644 test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs

diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs
new file mode 100644
index 0000000..6305c27
--- /dev/null
+++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/Helpers/DisposeHelperTests.cs
@@ -0,0 +1,123 @@
+// 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;
+
+namespace Microsoft.Testing.Platform.UnitTests;
+
+[TestClass]
+public sealed class DisposeHelperTests
+{
+    [TestMethod]
+    public async Task DisposeAsync_WithNull_DoesNotThrow()
+        => await DisposeHelper.DisposeAsync(null);
+
+    [TestMethod]
+    public async Task DisposeAsync_WithAsyncCleanableExtension_CallsCleanupAsync()
+    {
+        AsyncCleanableOnly obj = new();
+
+        await DisposeHelper.DisposeAsync(obj);
+
+        Assert.IsTrue(obj.CleanupCalled);
+    }
+
+#if NETCOREAPP
+    [TestMethod]
+    public async Task DisposeAsync_WithAsyncDisposable_CallsDisposeAsyncNotDispose()
+    {
+        AsyncDisposableAndDisposable obj = new();
+
+        await DisposeHelper.DisposeAsync(obj);
+
+        Assert.IsTrue(obj.DisposeAsyncCalled);
+        Assert.IsFalse(obj.DisposeCalled);
+    }
+

... (truncated)

Metadata

Metadata

Labels

area/agentic-workflowsGitHub agentic workflow definitions under .github/workflows/*.md.type/automationCreated or maintained by an agentic workflow.type/test-gapMissing or insufficient tests.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions