Skip to content

[test-improver] Add unit tests for ActivityWrapper SetTag, non-W3C TraceId/SpanId, IsRecording, and non-ambient Dispose #11433

Description

@nohwnd-bot

Goal

ActivityWrapper (Microsoft.Testing.Extensions.OpenTelemetry) wraps a System.Diagnostics.Activity to implement IPlatformActivity. Several of its branches had no direct or indirect coverage in OpenTelemetryPlatformServiceTests.cs:

  • SetTag (only ever exercised implicitly via StartActivity(tags:), never called directly, and its chaining return value was unchecked)
  • RecordException's additionalTags merge parameter
  • TraceId/SpanId returning null for non-W3C (ActivityIdFormat.Hierarchical) activities
  • IsRecording returning false (only ever observed as true, via this test class's own listener which always samples AllDataAndRecorded)
  • The non-ambient Dispose() branch that saves/restores Activity.Current around activity.Dispose()

Approach

Added 5 tests to the existing OpenTelemetryPlatformServiceTests.cs (keeping the file's existing pattern of exercising ActivityWrapper through the real OpenTelemetryPlatformService plus a live ActivityListener, rather than mocking):

  • SetTag_SetsTheTagAndReturnsTheSameActivityForChaining
  • RecordException_WithAdditionalTags_MergesThemOntoTheExceptionEvent
  • TraceIdAndSpanId_WhenActivityIsNotW3CFormat_AreNull
  • IsRecording_WhenNoListenerSamplesTheActivityForData_IsFalse — required a dedicated ActivitySource/ActivityListener pair sampling PropagationData (not AllDataAndRecorded), since the test class's shared listener always requests full data.
  • Dispose_ForANonAmbientActivity_RestoresThePreviousAmbientActivity

A small WrapNonAmbient(Activity) helper constructs ActivityWrapper directly (accessible via the project's existing InternalsVisibleTo) for the two tests that need to control activity state that OpenTelemetryPlatformService's public surface doesn't expose directly (a non-W3C-format activity, and a not-recorded activity).

Coverage impact

Before After
Microsoft.Testing.Extensions.UnitTests (net8.0) total tests 1940 1945
Failed 0 0
Skipped (pre-existing) 51 51

Trade-offs

Minimal — all 5 new tests are small, focused, and follow the file's existing conventions (real Activity/ActivityListener instances rather than mocks, matching the class's documented approach).

Reproducibility

./build.sh -restore
export PATH="$PWD/.dotnet:$PATH"
dotnet build test/UnitTests/Microsoft.Testing.Extensions.UnitTests/Microsoft.Testing.Extensions.UnitTests.csproj -c Debug -f net8.0
./artifacts/bin/Microsoft.Testing.Extensions.UnitTests/Debug/net8.0/Microsoft.Testing.Extensions.UnitTests

Test Status

  • Build: succeeded, 0 warnings, 0 errors.
  • Full Microsoft.Testing.Extensions.UnitTests net8.0 suite: 1945 total, 0 failed, 51 skipped (pre-existing, no regressions).
  • dotnet format whitespace TestFx.slnx --verify-no-changes --include <file>: clean (only the expected harmless F#-project warning unrelated to this change).

Note

This was originally intended as a pull request, but the git push operation failed.

Original error: The process '/usr/bin/git' failed with exit code 128

Workflow Run: View run details and download bundle artifact

The bundle file is available in the agent artifact in the workflow run linked above.

Create the pull request manually
# Download the artifact from the workflow run
gh run download '35665076505' -n agent -D '/tmp/agent-35665076505'

# Resolve the bundle source ref, fetch it into a temporary ref, then create the local branch
bundle_path='/tmp/agent-35665076505/aw-test-assist-activity-wrapper-tests.bundle'
temp_ref='refs/bundles/create-pr-test-assist-activity-wrapper-tests-e0a7a9358b21c21f-a362a391'
target_ref='refs/heads/test-assist/activity-wrapper-tests-e0a7a9358b21c21f'
bundle_source_ref=$(git bundle list-heads "$bundle_path" | awk '$2 ~ /^refs\/heads\// { print $2 }')
if [ -z "$bundle_source_ref" ]; then
  bundle_source_ref=$(git bundle list-heads "$bundle_path" | awk '$2 == "HEAD" { print $2 }')
fi
if [ "$(printf '%s\n' "$bundle_source_ref" | sed '/^$/d' | wc -l | tr -d ' ')" != "1" ]; then
  echo "Expected exactly one bundle source ref, found: $bundle_source_ref" >&2
  exit 1
fi
git fetch "$bundle_path" "${bundle_source_ref}:${temp_ref}"
git update-ref "$target_ref" "$temp_ref"
git checkout 'test-assist/activity-wrapper-tests-e0a7a9358b21c21f'
# Ensure the working tree matches the updated branch
git reset --hard
# Remove the temporary bundle ref
git update-ref -d "$temp_ref"

# Push the branch to the target remote
git push 'origin' 'test-assist/activity-wrapper-tests-e0a7a9358b21c21f'

# Create the pull request
gh pr create --title '[test-improver] Add unit tests for ActivityWrapper SetTag, non-W3C TraceId/SpanId, IsRecording, and non-ambient Dispose' --base 'main' --head 'test-assist/activity-wrapper-tests-e0a7a9358b21c21f' --repo 'microsoft/testfx'

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • github.com
  • southcentralus0.in.applicationinsights.azure.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "github.com"
    - "southcentralus0.in.applicationinsights.azure.com"

See Network Configuration for more information.

🤖 Automated content by GitHub Copilot. Generated by the Test Improver workflow. · copilot · auto · 245.1 AIC · ⌖ 23.5 AIC · ⊞ 17K · [◷]( · )
Comment /test-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/test-improver.md@main

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/mtp-observabilityMTP OpenTelemetry, telemetry, and logging extensions.needs/triageNeeds triage by a maintainer.type/test-gapMissing or insufficient tests.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions