Add base class for command unit tests#2399
Add base class for command unit tests#2399alzimmermsft wants to merge 6 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
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>inMicrosoft.Mcp.Testsplus 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. |
jongio
left a comment
There was a problem hiding this comment.
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.
|
|
||
| using System.CommandLine; | ||
| using System.Net; | ||
| using System.Text.Json; |
There was a problem hiding this comment.
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?
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
ServiceCollectionandServiceProviderto 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
servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationREADME.mdchanges running the script./eng/scripts/Process-PackageReadMe.ps1. See Package READMEToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.jsonbreaking-changelabelservers/Azure.Mcp.Server/docs/azmcp-commands.md./eng/scripts/Update-AzCommandsMetadata.ps1to update tool metadata inazmcp-commands.md(required for CI)servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline