PactNet 4.5.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer prerelease 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 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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

PactNet.Linux.x64 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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

PactNet.OSX The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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

PactNet.Linux.x86 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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-beta.2 12,734 2/15/2024
5.0.0-beta.1 87,414 6/29/2023
4.6.0-beta.1 458 5/2/2023
4.5.0 603,021 4/9/2023
4.4.0 221,904 2/12/2023
4.3.0 572,915 11/10/2022
4.2.1 148,981 9/18/2022
4.2.0 30,481 9/1/2022
4.1.0 105,620 7/13/2022
4.0.0 61,697 6/6/2022
4.0.0-beta.5 5,583 5/26/2022
4.0.0-beta.4 707 5/20/2022
4.0.0-beta.3 111,282 2/22/2022
4.0.0-beta.2 449 2/16/2022
4.0.0-beta.1 3,402 2/9/2022
3.0.2 903,698 10/11/2021
3.0.1 124,764 10/6/2021
3.0.0 399,672 5/14/2021
2.6.1 547,211 10/12/2020
2.6.0 6,070 10/11/2020
2.5.5 219,344 6/25/2020
2.5.4 238,996 5/6/2020
2.5.3 26,425 4/8/2020
2.5.2 69,475 3/5/2020
2.5.1 55,345 2/18/2020
2.5.0 78,438 1/14/2020
2.4.8 259,945 10/3/2019
2.4.7 28,818 8/27/2019
2.4.6 81,102 5/23/2019
2.4.5 259,257 11/29/2018
2.4.4 17,394 10/30/2018
2.4.3 70,924 10/9/2018
2.3.3 66,069 8/21/2018
2.3.2 9,837 8/14/2018
2.3.1 71,149 6/21/2018
2.3.0 36,787 5/12/2018
2.2.5 48,224 3/27/2018
2.2.4 26,492 3/10/2018
2.2.3 5,878 3/5/2018
2.2.2 43,566 2/25/2018
2.2.1 173,459 12/27/2017
2.2.0 79,110 11/26/2017
2.1.2 9,366 11/23/2017
2.1.1 7,092 11/11/2017
2.1.0 5,921 11/9/2017
2.0.21 6,385 11/7/2017
2.0.20 6,570 11/1/2017
2.0.19 14,570 10/19/2017
2.0.18 5,621 10/18/2017
2.0.17 18,938 9/6/2017
2.0.16-beta 5,012 9/5/2017
2.0.15-beta 778 9/2/2017
2.0.14-beta 827 8/30/2017
2.0.13-beta 787 8/29/2017
2.0.12-beta 1,268 8/28/2017
2.0.11-beta 1,007 8/21/2017
2.0.10-beta 783 8/20/2017
2.0.9-beta 748 8/20/2017
2.0.8-beta 737 8/20/2017
2.0.7-beta 1,343 8/2/2017
2.0.6-beta 938 7/30/2017
2.0.5-beta 32,678 7/21/2017
2.0.4-beta 1,185 7/16/2017
2.0.3-beta 912 7/15/2017
2.0.2-beta 3,132 7/11/2017
2.0.1-beta 976 7/4/2017
2.0.0-beta 1,004 7/2/2017
1.3.2 49,041 7/13/2017
1.3.1 34,151 6/25/2017
1.3.0 62,036 1/21/2017
1.3.0-beta 808 1/20/2017
1.2.0 6,413 11/9/2016
1.1.4 141,244 5/30/2016
1.1.3 1,494 5/11/2016
1.1.2 5,994 5/2/2016
1.1.1 12,610 1/10/2016
1.1.0 1,141 1/10/2016
1.0.11 4,118 10/5/2015
1.0.10 1,547 9/21/2015
1.0.9 1,515 9/3/2015
1.0.8 3,066 8/1/2015
1.0.7 99,298 7/16/2015
1.0.6 1,350 7/10/2015
1.0.5 2,552 6/27/2015
1.0.4 1,138 6/24/2015
1.0.3 1,692 6/4/2015
1.0.2 1,168 6/2/2015
1.0.1 1,221 5/30/2015
1.0.0 7,820 4/20/2015
1.0.0-beta 1,086 4/19/2015
0.1.11-beta 973 4/13/2015
0.1.10-beta 924 4/13/2015
0.1.9-beta 1,091 4/2/2015
0.1.8-beta 870 4/1/2015
0.1.7-beta 24,376 12/7/2014
0.1.6-beta 1,066 12/2/2014
0.1.5-beta 1,360 11/13/2014
0.1.4-beta 1,099 10/5/2014
0.1.3-beta 1,525 9/25/2014
0.1.2-beta 911 9/11/2014
0.1.1-beta 909 9/10/2014
0.1.0-beta 902 8/29/2014

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