Loading...
Loading...
Compare original and translation side by side
mcp-csharp-createmcp-csharp-debugmcp-csharp-debugmcp-csharp-createmcp-csharp-debugmcp-csharp-debug| Input | Required | Description |
|---|---|---|
| MCP server project path | Yes | Path to the server |
| Test framework | Recommended | Default: xUnit. Also supports NUnit or MSTest |
| Transport type | Recommended | Determines integration test approach (stdio vs HTTP) |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| MCP服务器项目路径 | 是 | 待测试服务器的 |
| 测试框架 | 推荐 | 默认:xUnit。同时支持NUnit或MSTest |
| 传输类型 | 推荐 | 决定集成测试的实现方式(stdio vs HTTP) |
dotnet new xunit -n <ServerName>.Tests
cd <ServerName>.Tests
dotnet add reference ../<ServerName>/<ServerName>.csproj
dotnet add package ModelContextProtocol
dotnet add package Moq
dotnet add package FluentAssertionsdotnet new xunit -n <ServerName>.Tests
cd <ServerName>.Tests
dotnet add reference ../<ServerName>/<ServerName>.csproj
dotnet add package ModelContextProtocol
dotnet add package Moq
dotnet add package FluentAssertionspublic class MyToolTests
{
[Fact]
public void Echo_ReturnsFormattedMessage()
{
var result = MyTools.Echo("Hello");
result.Should().Be("Echo: Hello");
}
[Theory]
[InlineData("")]
[InlineData(" ")]
public void Echo_HandlesEdgeCases(string input)
{
var result = MyTools.Echo(input);
result.Should().StartWith("Echo:");
}
}public class ApiToolTests
{
[Fact]
public async Task FetchData_ReturnsApiResponse()
{
var handler = new MockHttpMessageHandler("""{"id": 1}""");
var httpClient = new HttpClient(handler);
var result = await ApiTools.FetchData(httpClient, "resource-1");
result.Should().Contain("id");
}
}public class MyToolTests
{
[Fact]
public void Echo_ReturnsFormattedMessage()
{
var result = MyTools.Echo("Hello");
result.Should().Be("Echo: Hello");
}
[Theory]
[InlineData("")]
[InlineData(" ")]
public void Echo_HandlesEdgeCases(string input)
{
var result = MyTools.Echo(input);
result.Should().StartWith("Echo:");
}
}public class ApiToolTests
{
[Fact]
public async Task FetchData_ReturnsApiResponse()
{
var handler = new MockHttpMessageHandler("""{"id": 1}""");
var httpClient = new HttpClient(handler);
var result = await ApiTools.FetchData(httpClient, "resource-1");
result.Should().Contain("id");
}
}using ModelContextProtocol.Client;
public class ServerIntegrationTests : IAsyncLifetime
{
private McpClient _client = null!;
public async Task InitializeAsync()
{
var transport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "TestClient",
Command = "dotnet",
Arguments = ["run", "--project", "../<ServerName>/<ServerName>.csproj"]
});
_client = await McpClient.CreateAsync(transport);
}
public async Task DisposeAsync() => await _client.DisposeAsync();
[Fact]
public async Task Server_ListsExpectedTools()
{
var tools = await _client.ListToolsAsync();
tools.Should().Contain(t => t.Name == "echo");
}
[Fact]
public async Task Tool_ReturnsExpectedResult()
{
var result = await _client.CallToolAsync("echo",
new Dictionary<string, object?> { ["message"] = "Test" });
var text = result.Content.OfType<TextContentBlock>().First().Text;
text.Should().Contain("Test");
}
}ClientServerTestBaseWebApplicationFactoryusing ModelContextProtocol.Client;
public class ServerIntegrationTests : IAsyncLifetime
{
private McpClient _client = null!;
public async Task InitializeAsync()
{
var transport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "TestClient",
Command = "dotnet",
Arguments = ["run", "--project", "../<ServerName>/<ServerName>.csproj"]
});
_client = await McpClient.CreateAsync(transport);
}
public async Task DisposeAsync() => await _client.DisposeAsync();
[Fact]
public async Task Server_ListsExpectedTools()
{
var tools = await _client.ListToolsAsync();
tools.Should().Contain(t => t.Name == "echo");
}
[Fact]
public async Task Tool_ReturnsExpectedResult()
{
var result = await _client.CallToolAsync("echo",
new Dictionary<string, object?> { ["message"] = "Test" });
var text = result.Content.OfType<TextContentBlock>().First().Text;
text.Should().Contain("Test");
}
}ClientServerTestBaseWebApplicationFactoryundefinedundefinedundefinedundefinedListToolsAsync()CallToolAsync()dotnet testListToolsAsync()CallToolAsync()dotnet test| Pitfall | Solution |
|---|---|
Integration test hangs on | Server fails to start. Verify |
| Use the correct relative path to |
| Tests pass locally but fail in CI | Run |
Mocking | Mock |
| Full test suite runs are slow | Use |
| 问题 | 解决方案 |
|---|---|
集成测试在 | 服务器启动失败。先验证 |
| 使用从测试项目目录到 |
| 本地测试通过但CI环境测试失败 | 在执行测试前先运行 |
模拟 | 直接模拟 |
| 完整测试套件运行缓慢 | 开发阶段使用 |
mcp-csharp-createmcp-csharp-debugmcp-csharp-publishmcp-csharp-createmcp-csharp-debugmcp-csharp-publishClientServerTestBaseWebApplicationFactoryMockHttpMessageHandlerClientServerTestBaseWebApplicationFactoryMockHttpMessageHandler