Language

RetryAttribute Class

Definition

This attribute is used to set a retry count on a test method in case of failure.

[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class RetryAttribute : Microsoft.VisualStudio.TestTools.UnitTesting.RetryBaseAttribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
public sealed class RetryAttribute : Microsoft.VisualStudio.TestTools.UnitTesting.RetryBaseAttribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type RetryAttribute = class
    inherit RetryBaseAttribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)>]
type RetryAttribute = class
    inherit RetryBaseAttribute
Public NotInheritable Class RetryAttribute
Inherits RetryBaseAttribute
Inheritance
RetryAttribute
Attributes

Remarks

When applied to a test class, the attribute is used as a default for every test method declared on the class. A RetryAttribute placed directly on a test method takes precedence over a class-level one (the method-level value fully replaces the class-level value, regardless of whether it is larger or smaller). The attribute is not inherited: applying it to a base test class does not apply it to derived test classes.

A retry is performed while at least one Microsoft.VisualStudio.TestTools.UnitTesting.TestResult of the attempt has a Failed or Timeout outcome. Retrying stops as soon as an attempt produces no failed and no timed-out result. For a data-driven test (multiple results per attempt) this is an aggregate rule: an Inconclusive result (for example when the test calls Assert.Inconclusive()) only ends retrying when none of its sibling results in the same attempt failed or timed out; otherwise the attempt is still retried. The last attempt's results become the final outcome of the test.

This attribute retries within a single test-host run. It is independent from the Microsoft.Testing.Extensions.Retry--retry-failed-tests orchestrator, which re-invokes the whole test host. When both are enabled at the same time the attempt counts multiply (a test can run up to (MaxRetryAttempts + 1) x (--retry-failed-tests + 1) times), so avoid combining them unless that is intended.

Constructors

Name Description
RetryAttribute(Int32)

Initializes a new instance of the RetryAttribute class with the given number of max retries.

Properties

Name Description
BackoffType

Gets or sets the delay backoff type.

MaxRetryAttempts

Gets the number of retries that the test should make in case of failures. Note that before RetryAttribute is considered, the test was already executed once. This property determines the max number of retries after the first normal run.

MillisecondsDelayBetweenRetries

Gets or sets the delay, in milliseconds, between retries. This delay is also applied after the first run and before the first retry attempt.

Methods

Name Description
ExecuteAsync(RetryContext)

Retries the test method. The details of how retry is done is left to the derived classes. Note that a first run of the method was already executed and failed before this method is called.

(Inherited from RetryBaseAttribute)

Applies to