PHPackages                             lirik44/phpunit-retry-annotations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. lirik44/phpunit-retry-annotations

ActiveLibrary[Testing &amp; Quality](/categories/testing)

lirik44/phpunit-retry-annotations
=================================

Traits for retrying test methods and classes in PHPUnit, modified for selenium php-webdriver

v1.0.1(2y ago)03Apache-2.0PHPPHP ^7.1|^8.0

Since Nov 7Pushed 2y ago1 watchersCompare

[ Source](https://github.com/lirik44/phpunit-retry-annotations)[ Packagist](https://packagist.org/packages/lirik44/phpunit-retry-annotations)[ Docs](https://github.com/lirik44/phpunit-retry-annotations)[ RSS](/packages/lirik44-phpunit-retry-annotations/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

PHPUnit Retry
=============

[](#phpunit-retry)

Traits for retrying test methods and classes in PHPUnit.

Installation
------------

[](#installation)

```
composer require --dev lirik44/phpunit-retry-annotations

```

You should customise your SELENIUM\_HUB url in Project.

Configuring retries
-------------------

[](#configuring-retries)

### Retry using a specified number of retries

[](#retry-using-a-specified-number-of-retries)

```
/**
 * @retryAttempts 2
 */
class MyTest extends PHPUnit\Framework\TestCase
{
    use PHPUnitRetry\RetryTrait;

    public function testSomethingFlakeyTwice()
    {
        // Retry a flakey test up to two times
    }

    /**
     * @retryAttempts 3
     */
    public function testSomethingFlakeyThreeTimes()
    {
        // Retry a flakey test up to three times
    }
}
```

**NOTE:** "Attempts" represents the number of times a test is retried. Providing "@retryAttempts" a value of 0 has no effect, and would not retry.

### Retry until a specific duration has passed

[](#retry-until-a-specific-duration-has-passed)

```
/**
 * @retryForSeconds 90
 */
class MyTest extends PHPUnit\Framework\TestCase
{
    use PHPUnitRetry\RetryTrait;

    public function testSomethingFlakeyFor90Seconds()
    {
        // retries for 90 seconds
    }

    /**
     * @retryForSeconds 1800
     */
    public function testSomethingFlakeyFor30Minutes()
    {
        // retries for 30 minutes
    }
}
```

Configuring retry conditions
----------------------------

[](#configuring-retry-conditions)

### Retry only for certain exceptions

[](#retry-only-for-certain-exceptions)

By default, retrying happens when any exception other than `PHPUnit\Framework\IncompleteTestError` and `PHPUnit\Framework\SkippedTestError`is thrown.

Because you may not always want to retry, you can configure your test to only retry under certain conditions. For example, you can only retry if your tests throw a certain exception.

```
/**
 * @retryAttempts 3
 * @retryIfException MyApi\ResourceExhaustedException
 */
```

You can retry for multiple exceptions.

```
/**
 * @retryAttempts 3
 * @retryIfException MyApi\RateLimitExceededException
 * @retryIfException ServiceUnavailableException
 */
```

### Retry based on a custom method

[](#retry-based-on-a-custom-method)

For more complex logic surrounding whether you should retry, define a custom retry method:

```
/**
 * @retryAttempts 3
 * @retryIfMethod isRateLimitExceededException
 */
public function testWithCustomRetryMethod()
{
    // retries only if the method `isRateLimitExceededException` returns true.
}

/**
 * @param Exception $e
 */
private function isRateLimitExceededException(Exception $e)
{
    // Check if HTTP Status code is 429 "Too many requests"
    return ($e instanceof HttpException && $e->getStatusCode() == 429);
}
```

Define arbitrary arguments for your retry method by passing them into the annotation:

```
/**
 * @retryAttempts 3
 * @retryIfMethod exceptionStatusCode 429
 */
public function testWithCustomRetryMethod()
{
    // retries only if the method `exceptionStatusCode` returns true.
}

/**
 * @param Exception $e
 */
private function exceptionStatusCode(Exception $e, $statusCode)
{
    // Check if HTTP status code is $statusCode
    return ($e instanceof HttpException && $e->getStatusCode() == $statusCode);
}
```

Configuring delay
-----------------

[](#configuring-delay)

### Delay for a duration between each retry

[](#delay-for-a-duration-between-each-retry)

```
/**
 * @retryAttempts 3
 * @retryDelaySeconds 10
 */
```

### Delay for an amount increasing exponentially based on the retry attempt

[](#delay-for-an-amount-increasing-exponentially-based-on-the-retry-attempt)

```
/**
 * @retryAttempts 3
 * @retryDelayMethod exponentialBackoff
 */
```

The behavior of the `exponentialBackoff` method is to start at 1 second and increase to a maximum of 60 seconds. The maximum delay can be customized by supplying a second argument to the annotation

```
/**
 * This test will delay with exponential backoff, with a maximum delay of 10 minutes.
 *
 * @retryAttempts 30
 * @retryDelayMethod exponentialBackoff 600
 */
```

### Define a custom delay method

[](#define-a-custom-delay-method)

```
/**
 * @retryAttempts 3
 * @retryDelayMethod myCustomDelay
 */
public function testWithCustomDelay()
{
    // retries using the method `myCustomDelay`.
}

/**
 * @param int $attempt The current test attempt
 */
private function myCustomDelay($attempt)
{
    // Doubles the sleep each attempt, but not longer than 10 seconds.
    sleep(min($attempt * 2, 10));
}
```

Define arbitrary arguments for your delay function by passing them into the annotation:

```
/**
 * @retryAttempts 3
 * @retryDelayMethod myCustomDelay 10 60
 */
public function testWithCustomDelay()
{
    // retries using the method `myCustomDelay`.
}

/**
 * @param int $attempt The current test attempt.
 * @param int $multiplier Rate of exponential backoff delay.
 * @param int $maxDelay Maximum time to wait regardless of retry attempt.
 */
private function myCustomDelay($attempt, $multiplier, $maxDelay)
{
    // Increases exponentially
    sleep(min($attempt * $multiplier, $max));
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 65% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

2

Last Release

895d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/083fe47b858f78595e8a7924fdf5ff89603e1ac6753b2abd760a240d6322877c?d=identicon)[lirik44](/maintainers/lirik44)

---

Top Contributors

[![bshaffer](https://avatars.githubusercontent.com/u/103941?v=4)](https://github.com/bshaffer "bshaffer (13 commits)")[![theofidry](https://avatars.githubusercontent.com/u/5175937?v=4)](https://github.com/theofidry "theofidry (4 commits)")[![raszi](https://avatars.githubusercontent.com/u/76983?v=4)](https://github.com/raszi "raszi (3 commits)")

---

Tags

phpunittestretryphp-webdriver

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/lirik44-phpunit-retry-annotations/health.svg)

```
[![Health](https://phpackages.com/badges/lirik44-phpunit-retry-annotations/health.svg)](https://phpackages.com/packages/lirik44-phpunit-retry-annotations)
```

###  Alternatives

[ergebnis/phpunit-slow-test-detector

Provides facilities for detecting slow tests in phpunit/phpunit.

1468.1M72](/packages/ergebnis-phpunit-slow-test-detector)[ta-tikoma/phpunit-architecture-test

Methods for testing application architecture

10745.9M13](/packages/ta-tikoma-phpunit-architecture-test)[php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

1718.2M399](/packages/php-mock-php-mock-phpunit)[fr3d/swagger-assertions

Test your API requests and responses against your swagger definition

138850.9k5](/packages/fr3d-swagger-assertions)[bshaffer/phpunit-retry-annotations

Traits for retrying test methods and classes in PHPUnit

23482.5k2](/packages/bshaffer-phpunit-retry-annotations)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
