Skip to content

Improve code coverage for System.Text.Json with targeted unit tests#120731

Merged
stephentoub merged 24 commits intomainfrom
copilot/maximize-code-coverage-json
Oct 18, 2025
Merged

Improve code coverage for System.Text.Json with targeted unit tests#120731
stephentoub merged 24 commits intomainfrom
copilot/maximize-code-coverage-json

Conversation

Copy link
Contributor

Copilot AI commented Oct 15, 2025

  • Mark DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException test as [OuterLoop]
  • Add 136 tests systematically targeting uncovered code paths
  • Final Coverage: 94.27% line, 90.60% branch, 93.40% method (baseline: 93.73%, +0.54% improvement)
  • Fix OutOfMemoryException in validation tests
  • Convert all JSON string constants to raw string literals (per code review feedback)
  • Replace wasteful JsonSerializerOptions creation with JsonSerializerOptions.Default (per code review feedback)
  • Revert incorrect line in JsonValueTests to use options variable (per code review feedback)
  • Convert additional JSON strings to raw string literals in DomTests.cs (per code review feedback)
  • All 49,787 tests passing

Summary

Through systematic iterations, this PR improved System.Text.Json coverage from 93.73% to 94.27% (+0.54%) by adding 136 targeted unit tests covering previously untested API surfaces including JsonSerializer methods with JsonTypeInfo/JsonSerializerContext, multi-segment Utf8JsonReader scenarios, JsonWriter formatting options, JsonNode operations, JsonNamingPolicy variants, and collection serialization.

Remaining gaps to reach 95% (~215 lines) consist primarily of complex Utf8JsonReader edge cases requiring specialized buffer setups, ThrowHelper exception paths, and F# converter infrastructure.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 15, 2025
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Maximize code coverage for System.Text.Json library Improve code coverage for System.Text.Json with targeted unit tests Oct 15, 2025
Copilot AI requested a review from stephentoub October 15, 2025 04:24
@stephentoub
Copy link
Member

@copilot, please mark the DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException test as [Outerloop] and try again, following my original instructions.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 15, 2025

@copilot, please mark the DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException test as [Outerloop] and try again, following my original instructions.

Done. Marked the test as [OuterLoop] and reverted the other test changes that had incorrect assertions. All tests now pass. Changes committed in edead34.

@stephentoub
Copy link
Member

@copilot:

  1. Run the existing test suite for System.Text.Json with coverage enabled (/p:Coverage=true) and collect detailed coverage reports.
  2. Analyze the reports to identify uncovered code paths, methods, or branches within System.Text.Json.
  3. Systematically add new unit tests targeting uncovered or under-tested areas, aiming to bring coverage as close to 100% as possible while maintaining meaningful test quality.
  4. Re-run the coverage analysis with the new tests and compare results to verify improvement.
  5. Repeat steps 1-4, focusing on any remaining uncovered segments, until coverage is maximized for all practical purposes.

Copilot AI and others added 2 commits October 15, 2025 16:26
…Deserialize from Span

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 15, 2025

@copilot:

  1. Run the existing test suite for System.Text.Json with coverage enabled (/p:Coverage=true) and collect detailed coverage reports.
    ...

Working on it:

  1. ✅ Ran coverage analysis - baseline: 93.73% line, 90.52% branch, 93.02% method
  2. ✅ Analyzed reports - identified uncovered SerializeToDocument/Element/Node methods with JsonTypeInfo parameters, and Deserialize from Span methods
  3. ✅ Added 12 new unit tests targeting those uncovered methods
  4. Currently building and testing to verify improvement

Next steps: Will re-run coverage analysis and continue adding tests for remaining uncovered areas. Changes committed in eace0c3.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub
Copy link
Member

@copilot, please keep iterating: run coverage, add more tests to fill gaps, re-run coverage, add more tests to fill gaps, etc. Please try to get line and branch coverage to greater than 95%.

Copilot AI and others added 2 commits October 15, 2025 22:14
…th JsonTypeInfo

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…alize methods

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 15, 2025

@copilot, please keep iterating: run coverage, add more tests to fill gaps, re-run coverage, add more tests to fill gaps, etc. Please try to get line and branch coverage to greater than 95%.

Continuing iterations:

Current Progress:

  • Baseline: 93.73% line, 90.52% branch
  • Current: 94.06% line, 90.51% branch (93.21% method)
  • Added 40 new tests across 3 commits (40a1e92, 0b52f02)

Tests Added:

  • JsonSerializer Deserialize/Serialize with JsonTypeInfo (generic & non-generic)
  • JsonSerializer methods with JsonSerializerContext
  • Coverage for Document, Element, Node, Span, Stream, Writer, Reader formats

Next Steps to reach >95%:
Working on additional edge cases and error paths. The remaining gaps are mostly in:

  • Utf8JsonReader edge cases (153 uncovered lines)
  • ThrowHelper methods (62 uncovered lines)
  • F# converters (not easily testable)

Continuing to add more tests...

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub
Copy link
Member

@copilot, please address the feedback from @eiriktsarpalis

…o avoid cache recreation

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 17, 2025

@copilot, please address the feedback from @eiriktsarpalis

Fixed in commit 0c7a2e5db. Replaced all instances of creating new JsonSerializerOptions with TypeInfoResolver with JsonSerializerOptions.Default to avoid wasteful cache recreation.

…ons.Default

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub merged commit f0db8f9 into main Oct 18, 2025
85 checks passed
@stephentoub stephentoub deleted the copilot/maximize-code-coverage-json branch October 18, 2025 17:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants