Skip to content

feat: added param:returnWithLogs for run_tests, count pass/fail with valid tests only #48

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 1 commit into from
Jun 16, 2025

Conversation

yulcat
Copy link
Contributor

@yulcat yulcat commented Jun 16, 2025

Problem

When running tests, the logs generated during execution were not returned. As a result, an additional get_console_log call was required to retrieve them.

Even if the testFilter was incorrectly set and no actual test ran, the root test group was still marked as "Passed" with passCount: 1/1. This caused the LLM to wrongly assume that the test succeeded.

Fixes

Added a returnWithLogs parameter to the run_test request, allowing logs to be returned alongside the test result in a single response.

Updated the pass/fail count logic to only include actual test tasks. When testFilter is misconfigured, the result is now 0/0 instead of 1/1, prompting the LLM to recognize that no valid test was executed.

Implementation Details

The changes include:

  1. Added a new returnWithLogs parameter to the ExecuteTestsAsync method in the ITestRunnerService interface and its implementation in TestRunnerService.

  2. Modified the BuildResultJson method to:

    • Include logs in the test results when returnWithLogs is true
    • Add stack trace information to the results
    • Filter out test groups by using .Where(r => !r.HasChildren) to only count actual test tasks
    • Use the result's built-in counters (result.PassCount, result.FailCount, etc.) instead of manually counting
  3. Updated the parameter schemas in both JavaScript and TypeScript implementations to include the new returnWithLogs parameter with a default value of false.

  4. Improved the description of the testFilter parameter to clarify that it must include the namespace.

These changes ensure more accurate test reporting and reduce the need for additional API calls to retrieve test logs.

Created with Palmier

Summary by CodeRabbit

  • New Features
    • Added an option to include detailed test logs in test results. Users can now specify whether to receive logs alongside test outcomes when running tests.
  • Documentation
    • Updated parameter descriptions for improved clarity regarding test filtering and log inclusion options.

Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

📝 Walkthrough

Walkthrough

A new optional boolean parameter, returnWithLogs, was added throughout the test running toolchain to control whether test logs are included in the test results. This parameter is now supported in the TypeScript, JavaScript, and C# layers, with corresponding updates to method signatures, parameter schemas, and JSON output formatting.

Changes

File(s) Change Summary
Editor/Services/ITestRunnerService.cs Added returnWithLogs boolean parameter to ExecuteTestsAsync method and updated XML docs.
Editor/Services/TestRunnerService.cs Added _returnWithLogs field; updated ExecuteTestsAsync and BuildResultJson to support log inclusion.
Editor/Tools/RunTestsTool.cs Parsed new returnWithLogs parameter from input and forwarded it to test runner service.
Server~/build/tools/runTestsTool.js, Server~/src/tools/runTestsTool.ts Added returnWithLogs to parameter schema and handler; forwarded parameter in requests; improved docs for testFilter.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ToolJS/TS
    participant UnityTestRunnerService
    participant TestExecution

    Client->>ToolJS/TS: Request test run (with returnWithLogs)
    ToolJS/TS->>UnityTestRunnerService: ExecuteTestsAsync(..., returnWithLogs, ...)
    UnityTestRunnerService->>TestExecution: Run tests
    TestExecution-->>UnityTestRunnerService: Test results (+ logs if requested)
    UnityTestRunnerService-->>ToolJS/TS: JSON result (with or without logs)
    ToolJS/TS-->>Client: Final test result
Loading

Possibly related PRs

  • CoderGamester/mcp-unity#20: Modifies test result processing and reporting, particularly filtering and summary counts, directly related to the improved test result output structure in this PR.

Suggested reviewers

  • CoderGamester

Poem

In the warren of code, a new flag appears,
"returnWithLogs" now guides us through testy frontiers.
With logs or without, the results hop along,
JSONs enriched, the outputs grow strong.
🐇 With every test run, the story unfolds—
Now with more details, as the rabbit foretold!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ef8eac0 and bf76cee.

📒 Files selected for processing (5)
  • Editor/Services/ITestRunnerService.cs (1 hunks)
  • Editor/Services/TestRunnerService.cs (3 hunks)
  • Editor/Tools/RunTestsTool.cs (2 hunks)
  • Server~/build/tools/runTestsTool.js (2 hunks)
  • Server~/src/tools/runTestsTool.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (5)
  • Editor/Tools/RunTestsTool.cs
  • Editor/Services/ITestRunnerService.cs
  • Server~/build/tools/runTestsTool.js
  • Server~/src/tools/runTestsTool.ts
  • Editor/Services/TestRunnerService.cs
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch feature/return-with-logs
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
Editor/Tools/RunTestsTool.cs (1)

29-34: XML docs are stale – newly-supported parameters aren’t documented

ExecuteAsync now accepts returnOnlyFailures and returnWithLogs, but the <param> description still only mentions testMode and testFilter. Anyone reading the tooltip or generated docs will miss the new capabilities.

-/// <param name="parameters">Tool parameters, including optional 'testMode' and 'testFilter'.</param>
+/// <param name="parameters">
+/// Tool parameters. Supported keys:
+///  • testMode            – string (EditMode | PlayMode)
+///  • testFilter          – string
+///  • returnOnlyFailures  – bool
+///  • returnWithLogs      – bool
+/// </param>
Editor/Services/ITestRunnerService.cs (1)

24-29: Interface change looks good – but wording can mislead

The summary says “all logs” are included, but the implementation only injects per-test ITestResultAdaptor.Output, not the entire console log stream. Clarify to avoid confusion.

-/// <param name="returnWithLogs">If true, all logs are included in the output.</param>
+/// <param name="returnWithLogs">
+/// If true, the Unity `ITestResultAdaptor.Output` for each test is included in the
+/// JSON results.  (This does not include the full Editor console log.)
+/// </param>
Editor/Services/TestRunnerService.cs (1)

195-208: Avoid emitting "logs": null when logs are disabled

When _returnWithLogs is false, the serializer still writes "logs": null for every test.
Clients must then filter these noisy keys.

-["logs"]      = _returnWithLogs ? r.Output : null,
+// Only add the property when requested
+].Concat(
+    _returnWithLogs ? new[] { new JProperty("logs", r.Output) } : Array.Empty<JProperty>()
+))

(or build the object imperatively and add the property conditionally)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c709528 and ef8eac0.

📒 Files selected for processing (5)
  • Editor/Services/ITestRunnerService.cs (1 hunks)
  • Editor/Services/TestRunnerService.cs (3 hunks)
  • Editor/Tools/RunTestsTool.cs (2 hunks)
  • Server~/build/tools/runTestsTool.js (2 hunks)
  • Server~/src/tools/runTestsTool.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
Editor/Tools/RunTestsTool.cs (1)
Editor/Services/TestRunnerService.cs (1)
  • JObject (195-223)
Editor/Services/ITestRunnerService.cs (3)
Editor/Services/TestRunnerService.cs (1)
  • JObject (195-223)
Editor/Services/IConsoleLogsService.cs (1)
  • JObject (21-21)
Editor/Services/ConsoleLogsService.cs (1)
  • JObject (88-161)
🔇 Additional comments (5)
Editor/Tools/RunTestsTool.cs (2)

40-41: Null-coalescing default is fine – consider explicit validation

Parsing returnWithLogs mirrors the other booleans and is correct.
If stronger validation is desired (e.g. reject non-boolean JSON tokens instead of silently coercing), add a type-check here or rely on a schema upstream.


51-53: Call-site matches new interface signature

Parameter order aligns with ITestRunnerService.ExecuteTestsAsync. No further action.

Server~/build/tools/runTestsTool.js (1)

45-54: Propagation of new flag is correct

returnWithLogs is forwarded unchanged – implementation is sound.

Server~/src/tools/runTestsTool.ts (1)

56-63: Forwarding logic LGTM

Parameter plumbing is consistent; nothing further.

Editor/Services/TestRunnerService.cs (1)

210-221: Good fix for pass/fail counting

Switching to result.PassCount/FailCount accurately handles the “0/0 tests” edge-case – nice catch.

Comment on lines +8 to 11
testFilter: z.string().optional().default('').describe('The specific test filter to run (e.g. specific test name or class name, must include namespace) (optional)'),
returnOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)'),
returnWithLogs: z.boolean().optional().default(false).describe('Whether to return the test logs in the results (optional)')
});
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Default for returnOnlyFailures diverges from Editor default

Here the schema defaults returnOnlyFailures to true, while the Editor tool falls back to false.
With no explicit user input, the server will always request only failures, changing behaviour unexpectedly after this PR.

-    returnOnlyFailures: z.boolean().optional().default(true)
+    // Keep default aligned with Editor side (false)
+    returnOnlyFailures: z.boolean().optional().default(false)

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Server~/build/tools/runTestsTool.js around lines 8 to 11, the default value
for returnOnlyFailures is set to true, which conflicts with the Editor tool's
default of false. To fix this, change the default value of returnOnlyFailures in
the schema to false so that the server behavior aligns with the Editor tool when
no explicit user input is provided.

Comment on lines +13 to 16
testFilter: z.string().optional().default('').describe('The specific test filter to run (e.g. specific test name or class name, must include namespace) (optional)'),
returnOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)'),
returnWithLogs: z.boolean().optional().default(false).describe('Whether to return the test logs in the results (optional)')
});
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Same default mismatch as in JS build artifact

Align the TypeScript source with the Editor default to avoid accidental filtering.

-  returnOnlyFailures: z.boolean().optional().default(true)
+  returnOnlyFailures: z.boolean().optional().default(false)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
testFilter: z.string().optional().default('').describe('The specific test filter to run (e.g. specific test name or class name, must include namespace) (optional)'),
returnOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)'),
returnWithLogs: z.boolean().optional().default(false).describe('Whether to return the test logs in the results (optional)')
});
testFilter: z.string().optional().default('').describe('The specific test filter to run (e.g. specific test name or class name, must include namespace) (optional)'),
- returnOnlyFailures: z.boolean().optional().default(true).describe('Whether to show only failed tests in the results (optional)'),
+ returnOnlyFailures: z.boolean().optional().default(false).describe('Whether to show only failed tests in the results (optional)'),
returnWithLogs: z.boolean().optional().default(false).describe('Whether to return the test logs in the results (optional)')
});
🤖 Prompt for AI Agents
In Server~/src/tools/runTestsTool.ts around lines 13 to 16, the default value
for returnOnlyFailures is set to true, which mismatches the expected default in
the Editor. Change the default value of returnOnlyFailures from true to false to
align with the Editor default and prevent accidental filtering of test results.

@yulcat yulcat force-pushed the feature/return-with-logs branch from ef8eac0 to bf76cee Compare June 16, 2025 14:12
Copy link
Owner

@CoderGamester CoderGamester left a comment

Choose a reason for hiding this comment

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

Good improvement and proposal
LGTM

@CoderGamester CoderGamester merged commit 219eccc into CoderGamester:main Jun 16, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants