Skip to content

Add base class for command unit tests#2399

Open
alzimmermsft wants to merge 6 commits intomicrosoft:mainfrom
alzimmermsft:BaseClassForCommandUnitTests
Open

Add base class for command unit tests#2399
alzimmermsft wants to merge 6 commits intomicrosoft:mainfrom
alzimmermsft:BaseClassForCommandUnitTests

Conversation

@alzimmermsft
Copy link
Copy Markdown
Contributor

@alzimmermsft alzimmermsft commented Apr 14, 2026

What does this PR do?

This PR adds a base class for command unit tests to provide shared functionality that can be used by all command unit tests. The base class binds up a ServiceCollection and ServiceProvider to provide a richer testing experience that more closely aligns with how production runtime works. This leverages dependency injection used in runtime and should allow for future changes to the runtime to more easily integrate into all unit tests by providing a central location for configuration.

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline

@alzimmermsft alzimmermsft changed the title [PoC] Add base class for command unit tests Add base class for command unit tests Apr 15, 2026
@alzimmermsft alzimmermsft marked this pull request as ready for review April 15, 2026 21:58
@alzimmermsft alzimmermsft requested review from a team, jongio and xiangyan99 as code owners April 15, 2026 21:58
Copilot AI review requested due to automatic review settings April 15, 2026 21:58
@alzimmermsft alzimmermsft requested review from vukelich and wbreza April 15, 2026 21:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a shared CommandUnitTestsBase to centralize command-unit-test setup (DI + mocked services) and migrates multiple tool/core command unit tests to use it, reducing repeated boilerplate and aligning tests more closely with production DI patterns.

Changes:

  • Introduced CommandUnitTestsBase<ToolCommand, ToolService> in Microsoft.Mcp.Tests plus NSubstitute dependencies for that test utility project.
  • Refactored multiple command unit tests (Storage/AppConfig/AKS/Advisor/ACR + core Azure commands) to inherit from the new base and use shared helpers (ExecuteCommandAsync, ConvertResponse).
  • Updated contributor guidance (CONTRIBUTING/AGENTS) to document the expected test base class pattern.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.Storage/tests/Azure.Mcp.Tools.Storage.UnitTests/Table/TableListCommandTests.cs Migrates to shared command test base + helper execution/response conversion.
tools/Azure.Mcp.Tools.Storage/tests/Azure.Mcp.Tools.Storage.UnitTests/Account/AccountGetCommandTests.cs Migrates to shared command test base; reduces DI/parse boilerplate.
tools/Azure.Mcp.Tools.Storage/tests/Azure.Mcp.Tools.Storage.UnitTests/Account/AccountCreateCommandTests.cs Migrates to shared command test base; uses helper execution/response conversion.
tools/Azure.Mcp.Tools.AppConfig/tests/Azure.Mcp.Tools.AppConfig.UnitTests/KeyValue/KeyValueSetCommandTests.cs Migrates to shared command test base and shared helpers.
tools/Azure.Mcp.Tools.AppConfig/tests/Azure.Mcp.Tools.AppConfig.UnitTests/KeyValue/KeyValueGetCommandTests.cs Migrates to shared command test base and shared helpers.
tools/Azure.Mcp.Tools.AppConfig/tests/Azure.Mcp.Tools.AppConfig.UnitTests/KeyValue/KeyValueDeleteCommandTests.cs Migrates to shared command test base and shared helpers.
tools/Azure.Mcp.Tools.AppConfig/tests/Azure.Mcp.Tools.AppConfig.UnitTests/Account/AccountListCommandTests.cs Migrates to shared command test base and shared helpers.
tools/Azure.Mcp.Tools.Aks/tests/Azure.Mcp.Tools.Aks.UnitTests/Nodepool/NodepoolGetCommandTests.cs Migrates to shared command test base; simplifies execution & result parsing.
tools/Azure.Mcp.Tools.Aks/tests/Azure.Mcp.Tools.Aks.UnitTests/Cluster/ClusterGetCommandTests.cs Migrates to shared command test base; simplifies execution & result parsing.
tools/Azure.Mcp.Tools.Advisor/tests/Azure.Mcp.Tools.Advisor.UnitTests/Recommendation/RecommendationListCommandTests.cs Migrates to shared command test base; adds standard file header.
tools/Azure.Mcp.Tools.Acr/tests/Azure.Mcp.Tools.Acr.UnitTests/Registry/RegistryRepositoryListCommandTests.cs Migrates to shared command test base and shared helpers.
tools/Azure.Mcp.Tools.Acr/tests/Azure.Mcp.Tools.Acr.UnitTests/Registry/RegistryListCommandTests.cs Migrates to shared command test base and shared helpers.
core/Microsoft.Mcp.Core/tests/Microsoft.Mcp.Tests/Microsoft.Mcp.Tests.csproj Adds NSubstitute (+ analyzer) dependency needed by the new test base.
core/Microsoft.Mcp.Core/tests/Microsoft.Mcp.Tests/Client/CommandUnitTestsBase.cs New shared base class encapsulating DI/mocks + helpers for command tests.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/SubscriptionListCommandTests.cs Migrates to shared command test base and uses shared response conversion.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/SubscriptionCommandTests.cs Migrates to shared command test base; simplifies execution and mocks.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Server/Commands/ToolLoading/PluginTelemetryCommandTests.cs Minor DI registration simplification.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Group/UnitTests/ResourceListCommandTests.cs Migrates to shared command test base; simplifies error mocking and execution.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Group/UnitTests/ResourceGroupListCommandTests.cs Migrates to shared command test base; retains explicit parsing/serialization checks.
CONTRIBUTING.md Documents expectation that command unit tests use the shared base.
AGENTS.md Documents expected inheritance pattern for command unit tests.

Comment thread AGENTS.md Outdated
Comment thread CONTRIBUTING.md Outdated
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good refactoring - the DI-based base class is a solid improvement that eliminates a lot of boilerplate and better mirrors production runtime. A couple things to consider below.

Comment thread core/Microsoft.Mcp.Core/tests/Microsoft.Mcp.Tests/Client/CommandUnitTestsBase.cs Outdated
Comment thread core/Microsoft.Mcp.Core/tests/Microsoft.Mcp.Tests/Client/CommandUnitTestsBase.cs Outdated
Comment thread AGENTS.md Outdated

using System.CommandLine;
using System.Net;
using System.Text.Json;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file still uses the manual CommandDefinition.Parse + Command.ExecuteAsync pattern in all tests. The other migrated files (e.g., ResourceListCommandTests, AccountGetCommandTests) use ExecuteCommandAsync. Worth making consistent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

3 participants