PactNet 4.5.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.

Requires NuGet 3.0 or higher.

dotnet add package PactNet --version 4.5.0                
NuGet\Install-Package PactNet -Version 4.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PactNet" Version="4.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PactNet --version 4.5.0                
#r "nuget: PactNet, 4.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PactNet as a Cake Addin
#addin nuget:?package=PactNet&version=4.5.0

// Install PactNet as a Cake Tool
#tool nuget:?package=PactNet&version=4.5.0                

logo

Pact Net

Build status

Fast, easy and reliable testing for your APIs and microservices.

Pact is the de-facto API contract testing tool. Replace expensive and brittle end-to-end integration tests with fast, reliable and easy to debug unit tests.

  • ⚡ Lightning fast
  • 🎈 Effortless full-stack integration testing - from the front-end to the back-end
  • 🔌 Supports HTTP/REST and event-driven systems
  • 🛠️ Configurable mock server
  • 😌 Powerful matching rules prevents brittle tests
  • 🤝 Integrates with Pact Broker / PactFlow for powerful CI/CD workflows
  • 🔡 Supports 12+ languages

Why use Pact?

Contract testing with Pact lets you:

  • ⚡ Test locally
  • 🚀 Deploy faster
  • ⬇️ Reduce the lead time for change
  • 💰 Reduce the cost of API integration testing
  • 💥 Prevent breaking changes
  • 🔎 Understand your system usage
  • 📃 Document your APIs for free
  • 🗄 Remove the need for complex data fixtures
  • 🤷‍♂️ Reduce the reliance on complex test environments

Watch our series on the problems with end-to-end integrated tests, and how contract testing can help.

----------

Documentation

Tutorial (60 minutes)

Learn everything in Pact Net in 60 minutes

Upgrading from PactNet v3.x or earlier to v4.x

Upgrade Guide

Looking for PactNet v3.x? See the release/3.x branch.

Need Help

Installation

Via Nuget

----------

Usage

In the sections below, we provide a brief sample of the typical flow for Pact testing, written in the XUnit framework. To see the complete example and run it, check out the Samples/ReadMe folder.

Writing a Consumer test

Pact is a consumer-driven contract testing tool, which is a fancy way of saying that the API Consumer writes a test to set out its assumptions and needs of its API Provider(s). By unit testing our API client with Pact, it will produce a contract that we can share to our Provider to confirm these assumptions and prevent breaking changes.

In this example, we are going to be testing our User API client, responsible for communicating with the UserAPI over HTTP. It currently has a single method GetUser(id) that will return a User.

Pact tests have a few key properties. We'll demonstrate a common example using the 3A Arrange/Act/Assert pattern.

public class SomethingApiConsumerTests
{
    private readonly IPactBuilderV3 pactBuilder;

    public SomethingApiConsumerTests()
    {
        // Use default pact directory ..\..\pacts and default log
        // directory ..\..\logs
        var pact = Pact.V3("Something API Consumer", "Something API", new PactConfig());

        // or specify custom log and pact directories
        pact = Pact.V3("Something API Consumer", "Something API", new PactConfig
        {
            PactDir = $"{Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName}{Path.DirectorySeparatorChar}pacts"
        });

        // Initialize Rust backend
        this.pactBuilder = pact.WithHttpInteractions();
    }

    [Fact]
    public async Task GetSomething_WhenTheTesterSomethingExists_ReturnsTheSomething()
    {
        // Arrange
        this.pactBuilder
            .UponReceiving("A GET request to retrieve the something")
                .Given("There is a something with id 'tester'")
                .WithRequest(HttpMethod.Get, "/somethings/tester")
                .WithHeader("Accept", "application/json")
            .WillRespond()
                .WithStatus(HttpStatusCode.OK)
                .WithHeader("Content-Type", "application/json; charset=utf-8")
                .WithJsonBody(new
                {
                    id = "tester",
                    firstName = "Totally",
                    lastName = "Awesome"
                });

        await this.pactBuilder.VerifyAsync(async ctx =>
        {
            // Act
            var client = new SomethingApiClient(ctx.MockServerUri);
            var something = await client.GetSomething("tester");

            // Assert
            Assert.Equal("tester", something.Id);
        });
    }
}

----------

Verifying a Provider

A provider test takes one or more pact files (contracts) as input, and Pact verifies that your provider adheres to the contract. In the simplest case, you can verify a provider as per below. In SomethingApiFixture, the provider is started. In SomethingApiTests, the fixture is verified against the pact files.

public class SomethingApiFixture : IDisposable
{
    private readonly IHost server;
    public Uri ServerUri { get; }

    public SomethingApiFixture()
    {
        ServerUri = new Uri("http://localhost:9223");
        server = Host.CreateDefaultBuilder()
                     .ConfigureWebHostDefaults(webBuilder =>
                     {
                         webBuilder.UseUrls(ServerUri.ToString());
                         webBuilder.UseStartup<TestStartup>();
                     })
                     .Build();
        server.Start();
    }

    public void Dispose()
    {
        server.Dispose();
    }
}

public class SomethingApiTests : IClassFixture<SomethingApiFixture>
{
    private readonly SomethingApiFixture fixture;
    private readonly ITestOutputHelper output;

    public SomethingApiTests(SomethingApiFixture fixture, ITestOutputHelper output)
    {
        this.fixture = fixture;
        this.output = output;
    }

    [Fact]
    public void EnsureSomethingApiHonoursPactWithConsumer()
    {
        // Arrange
        var config = new PactVerifierConfig
        {
            Outputters = new List<IOutput>
            {
                // NOTE: PactNet defaults to a ConsoleOutput, however
                // xUnit 2 does not capture the console output, so this
                // sample creates a custom xUnit outputter. You will
                // have to do the same in xUnit projects.
                new XUnitOutput(output),
            },
        };

        string pactPath = Path.Combine("..",
                                       "..",
                                       "..",
                                       "..",
                                       "pacts",
                                       "Something API Consumer-Something API.json");

        // Act / Assert
        IPactVerifier pactVerifier = new PactVerifier(config);
        pactVerifier
            .ServiceProvider("Something API", fixture.ServerUri)
            .WithFileSource(new FileInfo(pactPath))
            .WithProviderStateUrl(new Uri(fixture.ServerUri, "/provider-states"))
            .Verify();
    }
}

IMPORTANT: You can't use the Microsoft.AspNetCore.Mvc.Testing library to host your API for provider tests. If your tests are using TestServer or WebApplicationFactory then these are running the API with a special in-memory test server instead of running on a real TCP socket. This means the Rust internals can't call the API and therefore all of your provider tests will fail. You must host the API on a proper TCP socket, e.g. by using the Host method shown in the sample above, so that they can be called from non-.Net code.

----------

Messaging Pacts

For writing messaging pacts instead of requests/response pacts, see the messaging pacts guide.

----------

Compatibility

Operating System

Due to using a shared native library instead of C# for the main Pact logic only certain OSs are supported:

OS Arch Support
Windows x86 ❌ No
Windows x64 ✔️ Yes
Linux (libc) ARM ❌ No
Linux (libc) x86 ❌ No
Linux (libc) x64 ✔️ Yes
Linux (musl) Any No
OSX x64 ✔️ Yes
OSX ARM (M1) ⚠️ Alpha

Pact Specification

Version Stable Spec Compatibility Install
4.x Stable 2, 3 See installation
3.x Deprecated 2

Roadmap

The roadmap for Pact and Pact Net is outlined on our main website.

Contributing

See CONTRIBUTING.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on PactNet:

Package Downloads
PactNet.Windows

A .NET version of Pact, which enables consumer driven contract testing. This package contains the Windows specific core engine.

PactNet.Linux.x64

A .NET version of Pact, which enables consumer driven contract testing. This package contains the 64-bit Linux specific core engine.

PactNet.OSX

A .NET version of Pact, which enables consumer driven contract testing. This package contains the Mac OSX specific core engine.

PactNet.Linux.x86

A .NET version of Pact, which enables consumer driven contract testing. This package contains the 32-bit Linux specific core engine.

seek.automation.stub

A library to allow stubbing of services while building integration or pact automated tests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 44,039 10/6/2024
5.0.0-beta.3 1,263 9/8/2024
5.0.0-beta.2 119,295 2/15/2024
5.0.0-beta.1 146,051 6/29/2023
4.6.0-beta.1 547 5/2/2023 4.6.0-beta.1 is deprecated.
4.5.0 1,787,020 4/9/2023
4.4.0 284,160 2/12/2023
4.3.0 698,037 11/10/2022
4.2.1 170,127 9/18/2022
4.2.0 37,548 9/1/2022
4.1.0 117,315 7/13/2022
4.0.0 73,489 6/6/2022
4.0.0-beta.5 5,711 5/26/2022 4.0.0-beta.5 is deprecated because it is no longer maintained.
4.0.0-beta.4 791 5/20/2022 4.0.0-beta.4 is deprecated because it is no longer maintained.
4.0.0-beta.3 113,651 2/22/2022 4.0.0-beta.3 is deprecated because it is no longer maintained.
4.0.0-beta.2 485 2/16/2022 4.0.0-beta.2 is deprecated because it is no longer maintained.
4.0.0-beta.1 7,534 2/9/2022 4.0.0-beta.1 is deprecated because it is no longer maintained.
3.0.2 1,145,851 10/11/2021 3.0.2 is deprecated because it is no longer maintained.
3.0.1 136,978 10/6/2021 3.0.1 is deprecated because it is no longer maintained.
3.0.0 426,183 5/14/2021 3.0.0 is deprecated because it is no longer maintained.
2.6.1 580,464 10/12/2020 2.6.1 is deprecated because it is no longer maintained.
2.6.0 6,280 10/11/2020 2.6.0 is deprecated because it is no longer maintained.
2.5.5 225,514 6/25/2020 2.5.5 is deprecated because it is no longer maintained.
2.5.4 242,457 5/6/2020 2.5.4 is deprecated because it is no longer maintained.
2.5.3 27,473 4/8/2020 2.5.3 is deprecated because it is no longer maintained.
2.5.2 72,577 3/5/2020 2.5.2 is deprecated because it is no longer maintained.
2.5.1 55,608 2/18/2020 2.5.1 is deprecated because it is no longer maintained.
2.5.0 79,150 1/14/2020 2.5.0 is deprecated because it is no longer maintained.
2.4.8 266,239 10/3/2019 2.4.8 is deprecated because it is no longer maintained.
2.4.7 30,098 8/27/2019 2.4.7 is deprecated because it is no longer maintained.
2.4.6 84,275 5/23/2019 2.4.6 is deprecated because it is no longer maintained.
2.4.5 262,817 11/29/2018 2.4.5 is deprecated because it is no longer maintained.
2.4.4 18,016 10/30/2018 2.4.4 is deprecated because it is no longer maintained.
2.4.3 71,336 10/9/2018 2.4.3 is deprecated because it is no longer maintained.
2.3.3 67,086 8/21/2018 2.3.3 is deprecated because it is no longer maintained.
2.3.2 10,333 8/14/2018 2.3.2 is deprecated because it is no longer maintained.
2.3.1 72,993 6/21/2018 2.3.1 is deprecated because it is no longer maintained.
2.3.0 37,145 5/12/2018 2.3.0 is deprecated because it is no longer maintained.
2.2.5 48,745 3/27/2018 2.2.5 is deprecated because it is no longer maintained.
2.2.4 27,013 3/10/2018 2.2.4 is deprecated because it is no longer maintained.
2.2.3 6,093 3/5/2018 2.2.3 is deprecated because it is no longer maintained.
2.2.2 43,855 2/25/2018 2.2.2 is deprecated because it is no longer maintained.
2.2.1 181,400 12/27/2017 2.2.1 is deprecated because it is no longer maintained.
2.2.0 85,263 11/26/2017 2.2.0 is deprecated because it is no longer maintained.
2.1.2 10,485 11/23/2017 2.1.2 is deprecated because it is no longer maintained.
2.1.1 7,892 11/11/2017 2.1.1 is deprecated because it is no longer maintained.
2.1.0 6,307 11/9/2017 2.1.0 is deprecated because it is no longer maintained.
2.0.21 6,759 11/7/2017 2.0.21 is deprecated because it is no longer maintained.
2.0.20 7,057 11/1/2017 2.0.20 is deprecated because it is no longer maintained.
2.0.19 14,833 10/19/2017 2.0.19 is deprecated because it is no longer maintained.
2.0.18 5,831 10/18/2017 2.0.18 is deprecated because it is no longer maintained.
2.0.17 20,253 9/6/2017 2.0.17 is deprecated because it is no longer maintained.
2.0.16-beta 5,191 9/5/2017 2.0.16-beta is deprecated because it is no longer maintained.
2.0.15-beta 812 9/2/2017 2.0.15-beta is deprecated because it is no longer maintained.
2.0.14-beta 862 8/30/2017 2.0.14-beta is deprecated because it is no longer maintained.
2.0.13-beta 818 8/29/2017 2.0.13-beta is deprecated because it is no longer maintained.
2.0.12-beta 1,315 8/28/2017 2.0.12-beta is deprecated because it is no longer maintained.
2.0.11-beta 1,044 8/21/2017 2.0.11-beta is deprecated because it is no longer maintained.
2.0.10-beta 815 8/20/2017 2.0.10-beta is deprecated because it is no longer maintained.
2.0.9-beta 784 8/20/2017 2.0.9-beta is deprecated because it is no longer maintained.
2.0.8-beta 773 8/20/2017 2.0.8-beta is deprecated because it is no longer maintained.
2.0.7-beta 1,397 8/2/2017 2.0.7-beta is deprecated because it is no longer maintained.
2.0.6-beta 974 7/30/2017 2.0.6-beta is deprecated because it is no longer maintained.
2.0.5-beta 32,724 7/21/2017 2.0.5-beta is deprecated because it is no longer maintained.
2.0.4-beta 1,240 7/16/2017 2.0.4-beta is deprecated because it is no longer maintained.
2.0.3-beta 965 7/15/2017 2.0.3-beta is deprecated because it is no longer maintained.
2.0.2-beta 3,232 7/11/2017 2.0.2-beta is deprecated because it is no longer maintained.
2.0.1-beta 1,014 7/4/2017 2.0.1-beta is deprecated because it is no longer maintained.
2.0.0-beta 1,045 7/2/2017 2.0.0-beta is deprecated because it is no longer maintained.
1.3.2 50,358 7/13/2017 1.3.2 is deprecated because it is no longer maintained.
1.3.1 35,067 6/25/2017 1.3.1 is deprecated because it is no longer maintained.
1.3.0 63,741 1/21/2017 1.3.0 is deprecated because it is no longer maintained.
1.3.0-beta 845 1/20/2017 1.3.0-beta is deprecated because it is no longer maintained.
1.2.0 6,685 11/9/2016 1.2.0 is deprecated because it is no longer maintained.
1.1.4 141,914 5/30/2016 1.1.4 is deprecated because it is no longer maintained.
1.1.3 1,546 5/11/2016 1.1.3 is deprecated because it is no longer maintained.
1.1.2 6,042 5/2/2016 1.1.2 is deprecated because it is no longer maintained.
1.1.1 13,013 1/10/2016 1.1.1 is deprecated because it is no longer maintained.
1.1.0 1,188 1/10/2016 1.1.0 is deprecated because it is no longer maintained.
1.0.11 4,789 10/5/2015 1.0.11 is deprecated because it is no longer maintained.
1.0.10 1,631 9/21/2015 1.0.10 is deprecated because it is no longer maintained.
1.0.9 1,559 9/3/2015 1.0.9 is deprecated because it is no longer maintained.
1.0.8 3,374 8/1/2015 1.0.8 is deprecated because it is no longer maintained.
1.0.7 108,835 7/16/2015 1.0.7 is deprecated because it is no longer maintained.
1.0.6 1,403 7/10/2015 1.0.6 is deprecated because it is no longer maintained.
1.0.5 2,786 6/27/2015 1.0.5 is deprecated because it is no longer maintained.
1.0.4 1,180 6/24/2015 1.0.4 is deprecated because it is no longer maintained.
1.0.3 1,735 6/4/2015 1.0.3 is deprecated because it is no longer maintained.
1.0.2 1,211 6/2/2015 1.0.2 is deprecated because it is no longer maintained.
1.0.1 1,266 5/30/2015 1.0.1 is deprecated because it is no longer maintained.
1.0.0 13,076 4/20/2015 1.0.0 is deprecated because it is no longer maintained.
1.0.0-beta 1,128 4/19/2015 1.0.0-beta is deprecated because it is no longer maintained.
0.1.11-beta 1,013 4/13/2015 0.1.11-beta is deprecated because it is no longer maintained.
0.1.10-beta 962 4/13/2015 0.1.10-beta is deprecated because it is no longer maintained.
0.1.9-beta 1,128 4/2/2015 0.1.9-beta is deprecated because it is no longer maintained.
0.1.8-beta 908 4/1/2015 0.1.8-beta is deprecated because it is no longer maintained.
0.1.7-beta 24,418 12/7/2014 0.1.7-beta is deprecated because it is no longer maintained.
0.1.6-beta 1,101 12/2/2014 0.1.6-beta is deprecated because it is no longer maintained.
0.1.5-beta 1,393 11/13/2014 0.1.5-beta is deprecated because it is no longer maintained.
0.1.4-beta 1,133 10/5/2014 0.1.4-beta is deprecated because it is no longer maintained.
0.1.3-beta 1,618 9/25/2014 0.1.3-beta is deprecated because it is no longer maintained.
0.1.2-beta 946 9/11/2014 0.1.2-beta is deprecated because it is no longer maintained.
0.1.1-beta 945 9/10/2014 0.1.1-beta is deprecated because it is no longer maintained.
0.1.0-beta 933 8/29/2014 0.1.0-beta is deprecated because it is no longer maintained.

v4.5.0
         - Provider state options
         - Use matchers with query strings
         - xUnit output project