Skip to content

Add returnsOnlyFailures option into run_tests tool #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Editor/Tools/RunTestsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class RunTestsTool : McpToolBase, ICallbacks
private readonly ITestRunnerService _testRunnerService;

private bool _isRunning = false;
private bool _returnsOnlyFailures = true;
private TaskCompletionSource<JObject> _testRunCompletionSource;
private List<TestResult> _testResults = new List<TestResult>();

Expand All @@ -31,6 +32,7 @@ private class TestResult
public string Message { get; set; }
public double Duration { get; set; }
public bool Passed => ResultState == "Passed";
public bool Skipped => ResultState.StartsWith("Skipped");
}

public RunTestsTool(ITestRunnerService testRunnerService)
Expand Down Expand Up @@ -65,6 +67,7 @@ public override void ExecuteAsync(JObject parameters, TaskCompletionSource<JObje
// Extract parameters
string testModeStr = parameters["testMode"]?.ToObject<string>() ?? "editmode";
string testFilter = parameters["testFilter"]?.ToObject<string>() ?? "";
_returnsOnlyFailures = parameters["returnsOnlyFailures"]?.ToObject<bool>() ?? true;

// Parse test mode
TestMode testMode;
Expand Down Expand Up @@ -157,6 +160,12 @@ public void RunFinished(ITestResultAdaptor result)
var resultArray = new JArray();
foreach (var testResult in _testResults)
{
// If returnsOnlyFailures is true, only include failed tests
if (_returnsOnlyFailures && (testResult.Passed || testResult.Skipped))
{
continue;
}

resultArray.Add(new JObject
{
["name"] = testResult.Name,
Expand Down
9 changes: 6 additions & 3 deletions Server/build/tools/runTestsTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const toolName = 'run_tests';
const toolDescription = 'Runs Unity\'s Test Runner tests';
const paramsSchema = z.object({
testMode: z.string().optional().describe('The test mode to run (EditMode or PlayMode) - defaults to EditMode (optional)'),
testFilter: z.string().optional().describe('The specific test filter to run (e.g. specific test name or namespace) (optional)')
testFilter: z.string().optional().describe('The specific test filter to run (e.g. specific test name or namespace) (optional)'),
returnsOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)')
});
/**
* Creates and registers the Run Tests tool with the MCP server
Expand Down Expand Up @@ -40,13 +41,14 @@ export function createRunTestsTool(server, mcpUnity, logger) {
* @throws McpUnityError if the request to Unity fails
*/
async function toolHandler(mcpUnity, params) {
const { testMode = 'EditMode', testFilter } = params;
const { testMode = 'EditMode', testFilter, returnsOnlyFailures = true } = params;
// Create and wait for the test run
const response = await mcpUnity.sendRequest({
method: toolName,
params: {
testMode,
testFilter
testFilter,
returnsOnlyFailures
}
});
// Process the test results
Expand All @@ -65,6 +67,7 @@ async function toolHandler(mcpUnity, params) {
if (testCount > 0 && passCount < testCount) {
resultMessage += `. Failed tests: ${testResults
.filter((r) => r.result !== 'Passed')
.filter((r) => !r.result.startsWith('Skipped'))
.map((r) => r.name)
.join(', ')}`;
}
Expand Down
9 changes: 6 additions & 3 deletions Server/src/tools/runTestsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const toolName = 'run_tests';
const toolDescription = 'Runs Unity\'s Test Runner tests';
const paramsSchema = z.object({
testMode: z.string().optional().describe('The test mode to run (EditMode or PlayMode) - defaults to EditMode (optional)'),
testFilter: z.string().optional().describe('The specific test filter to run (e.g. specific test name or namespace) (optional)')
testFilter: z.string().optional().describe('The specific test filter to run (e.g. specific test name or namespace) (optional)'),
returnsOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)')
});

/**
Expand Down Expand Up @@ -52,14 +53,15 @@ export function createRunTestsTool(server: McpServer, mcpUnity: McpUnity, logger
* @throws McpUnityError if the request to Unity fails
*/
async function toolHandler(mcpUnity: McpUnity, params: any): Promise<CallToolResult> {
const { testMode = 'EditMode', testFilter } = params;
const { testMode = 'EditMode', testFilter, returnsOnlyFailures = true } = params;

// Create and wait for the test run
const response = await mcpUnity.sendRequest({
method: toolName,
params: {
testMode,
testFilter
testFilter,
returnsOnlyFailures
}
});

Expand All @@ -84,6 +86,7 @@ async function toolHandler(mcpUnity: McpUnity, params: any): Promise<CallToolRes
if (testCount > 0 && passCount < testCount) {
resultMessage += `. Failed tests: ${testResults
.filter((r: any) => r.result !== 'Passed')
.filter((r: any) => !r.result.startsWith('Skipped'))
.map((r: any) => r.name)
.join(', ')}`;
}
Expand Down