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 3,651 2/15/2024
5.0.0-beta.1 78,401 6/29/2023
4.6.0-beta.1 452 5/2/2023
4.5.0 537,997 4/9/2023
4.4.0 210,872 2/12/2023
4.3.0 554,933 11/10/2022
4.2.1 145,864 9/18/2022
4.2.0 29,564 9/1/2022
4.1.0 104,180 7/13/2022
4.0.0 60,655 6/6/2022
4.0.0-beta.5 5,455 5/26/2022
4.0.0-beta.4 702 5/20/2022
4.0.0-beta.3 110,847 2/22/2022
4.0.0-beta.2 449 2/16/2022
4.0.0-beta.1 2,834 2/9/2022
3.0.2 870,851 10/11/2021
3.0.1 122,260 10/6/2021
3.0.0 394,394 5/14/2021
2.6.1 541,511 10/12/2020
2.6.0 5,595 10/11/2020
2.5.5 217,714 6/25/2020
2.5.4 237,821 5/6/2020
2.5.3 25,687 4/8/2020
2.5.2 68,506 3/5/2020
2.5.1 54,749 2/18/2020
2.5.0 77,824 1/14/2020
2.4.8 258,363 10/3/2019
2.4.7 28,020 8/27/2019
2.4.6 80,228 5/23/2019
2.4.5 258,397 11/29/2018
2.4.4 16,740 10/30/2018
2.4.3 70,285 10/9/2018
2.3.3 65,325 8/21/2018
2.3.2 9,243 8/14/2018
2.3.1 70,452 6/21/2018
2.3.0 36,103 5/12/2018
2.2.5 47,664 3/27/2018
2.2.4 26,004 3/10/2018
2.2.3 5,203 3/5/2018
2.2.2 42,949 2/25/2018
2.2.1 171,853 12/27/2017
2.2.0 77,706 11/26/2017
2.1.2 8,594 11/23/2017
2.1.1 6,450 11/11/2017
2.1.0 5,179 11/9/2017
2.0.21 5,737 11/7/2017
2.0.20 5,921 11/1/2017
2.0.19 13,972 10/19/2017
2.0.18 5,014 10/18/2017
2.0.17 18,184 9/6/2017
2.0.16-beta 4,348 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 48,970 7/13/2017
1.3.1 34,005 6/25/2017
1.3.0 61,963 1/21/2017
1.3.0-beta 808 1/20/2017
1.2.0 6,409 11/9/2016
1.1.4 141,224 5/30/2016
1.1.3 1,490 5/11/2016
1.1.2 5,993 5/2/2016
1.1.1 12,566 1/10/2016
1.1.0 1,140 1/10/2016
1.0.11 4,061 10/5/2015
1.0.10 1,542 9/21/2015
1.0.9 1,514 9/3/2015
1.0.8 3,058 8/1/2015
1.0.7 97,996 7/16/2015
1.0.6 1,349 7/10/2015
1.0.5 2,540 6/27/2015
1.0.4 1,135 6/24/2015
1.0.3 1,691 6/4/2015
1.0.2 1,167 6/2/2015
1.0.1 1,220 5/30/2015
1.0.0 7,640 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,374 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,500 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