Skip to content

[Test Improver] Add unit tests for RetryAttribute #7824

@github-actions

Description

@github-actions

🤖 This PR was created by Test Improver, an automated AI assistant focused on improving tests.

Goal and Rationale

RetryAttribute has meaningful logic: constructor validation (rejects maxRetryAttempts < 1), BackoffType setter validation, and an ExecuteAsync retry loop that stops early when a retry succeeds. This logic was covered only at the integration-test level — no unit tests existed in TestFramework.UnitTests.

Approach

Added 13 unit tests in RetryAttributeTests.cs following the TestContainer pattern used in the same test project:

Scenario Result
RetryAttribute(0) ArgumentOutOfRangeException
RetryAttribute(-1) ArgumentOutOfRangeException
RetryAttribute(1) MaxRetryAttempts = 1
RetryAttribute(5) MaxRetryAttempts = 5
Default BackoffType Constant
BackoffType = (DelayBackoffType)99 ArgumentOutOfRangeException
BackoffType = Exponential succeeds
Default MillisecondsDelayBetweenRetries 0
Test passes on first retry Stops early (1 call)
All 3 retries fail Exactly 3 calls
Second retry times out Last result is Timeout
Test passes after 2 failures Stops at attempt 3
First retry is Inconclusive Stops (not Failed/Timeout)

Test Status

✅ All 13 new tests pass. 796 existing tests still pass (809 total).

Test run summary: Passed!
  total: 809
  failed: 0
  succeeded: 809
  duration: ~3.4s

Reproducibility

export PATH="$PATH:.dotnet"
dotnet test test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj \
  -c Debug -f net8.0 -p:UseSharedCompilation=false

Note: -p:UseSharedCompilation=false is required in this environment due to a shared Roslyn compiler server sandbox restriction that prevents it from accessing NuGet package DLLs. CI should work normally.

Trade-offs

Low maintenance burden: tests follow the established TestContainer pattern, use no mocking frameworks, and directly cover the public/protected-internal contract of RetryAttribute. The #pragma warning disable MSTESTEXP suppresses the experimental API warning on RetryContext and ExecuteAsync.

Generated by Daily Test Improver · ● 8.3M ·


Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch test-assist/retry-attribute-unit-tests-e5b935ffdf7c54ee.

Click here to create the pull request

To fix the permissions issue, go to SettingsActionsGeneral and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (205 of 205 lines)
From 235d0534e320d4a713e10ec7d80bfcd26c45d630 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Sat, 25 Apr 2026 00:18:30 +0000
Subject: [PATCH] Add unit tests for RetryAttribute

Add 13 unit tests covering RetryAttribute constructor validation,
BackoffType property validation, and ExecuteAsync retry logic:
- Constructor rejects maxRetryAttempts < 1
- BackoffType setter rejects invalid enum values
- ExecuteAsync stops early when retry succeeds
- ExecuteAsync runs exactly MaxRetryAttempts times on all failures
- ExecuteAsync stops on Inconclusive result

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 .../Attributes/RetryAttributeTests.cs         | 176 ++++++++++++++++++
 1 file changed, 176 insertions(+)
 create mode 100644 test/UnitTests/TestFramework.UnitTests/Attributes/RetryAttributeTests.cs

diff --git a/test/UnitTests/TestFramework.UnitTests/Attributes/RetryAttributeTests.cs b/test/UnitTests/TestFramework.UnitTests/Attributes/RetryAttributeTests.cs
new file mode 100644
index 0000000..f775b00
--- /dev/null
+++ b/test/UnitTests/TestFramework.UnitTests/Attributes/RetryAttributeTests.cs
@@ -0,0 +1,176 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+
+#pragma warning disable MSTESTEXP // Experimental API
+
+using AwesomeAssertions;
+
+using TestFramework.ForTestingMSTest;
+
+namespace UnitTestFramework.Tests;
+
+/// <summary>
+/// Tests for <see cref="RetryAttribute"/> constructor validation and retry execution logic.
+/// </summary>
+public class RetryAttributeTests : TestContainer
+{
+    public void Constructor_WhenMaxRetryAttemptsIsZero_ThrowsArgumentOutOfRangeException()
+    {
+        Action act = static () => _ = new RetryAttribute(0);
+        act.Should().Throw<ArgumentOutOfRangeException>()
+            .WithParameterName("maxRetryAttempts");
+    }
+
+  
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions