Skip to main content
This document explains how to test Polly’s resilience pipelines. You should not test how the resilience pipelines operate internally, but rather test your own settings or custom delegates. To make the testing process simpler, Polly offers the Polly.Testing package. This package has a range of APIs designed to help you test the setup and combination of resilience pipelines in your user code.

Installation

Begin by adding the Polly.Testing package to your test project:

Basic Usage

Use the GetPipelineDescriptor extension method to get the ResiliencePipelineDescriptor which provides details on the pipeline’s composition:

Testing Generic Pipelines

The GetPipelineDescriptor extension method is also available for the generic ResiliencePipeline<T>:

Mocking ResiliencePipelineProvider

Consider the following code that might resemble a part of your project:
In the example above:
  • The MyApi class is introduced, representing part of your application that requires resilience support.
  • The AddMyApi extension method is also defined, which integrates MyApi into dependency injection (DI) and sets up the resilience pipeline it uses.

Mocking Example

For unit tests, if you want to assess the behavior of ExecuteAsync, it might not be practical to rely on the entire pipeline, especially since it could slow down tests during failure scenario evaluations. Instead, it’s recommended to mock the ResiliencePipelineProvider<string> and return an empty pipeline:
This example leverages the NSubstitute library to mock the pipeline provider.

Best Practices

Test Configuration

Focus on testing your resilience pipeline configuration and custom delegates, not the internal behavior of Polly strategies.

Use Descriptors

Use GetPipelineDescriptor() to verify that your pipeline is composed correctly with the right strategies and options.

Mock in Unit Tests

Mock ResiliencePipelineProvider in unit tests to avoid slow tests and focus on testing your application logic.

Integration Tests

Use real resilience pipelines in integration tests to verify end-to-end behavior with actual resilience strategies.