PactNet 4.3.0
Prefix ReservedSee the version list below for details.
Requires NuGet 3.0 or higher.
dotnet add package PactNet --version 4.3.0
NuGet\Install-Package PactNet -Version 4.3.0
<PackageReference Include="PactNet" Version="4.3.0" />
paket add PactNet --version 4.3.0
#r "nuget: PactNet, 4.3.0"
// Install PactNet as a Cake Addin #addin nuget:?package=PactNet&version=4.3.0 // Install PactNet as a Cake Tool #tool nuget:?package=PactNet&version=4.3.0
<span align="center">
Pact Net
Fast, easy and reliable testing for your APIs and microservices.
</span>
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
Looking for PactNet v3.x? See the release/3.x
branch.
Need Help
- Join our community slack workspace.
- Stack Overflow: https://stackoverflow.com/questions/tagged/pact
- Say 👋 on Twitter @pact_up
Installation
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 | Versions 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. |
-
.NETStandard 2.0
- PactNet.Abstractions (>= 4.3.0)
- System.Net.Http (>= 4.3.4)
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 | 33,535 | 10/6/2024 | |
5.0.0-beta.3 | 1,186 | 9/8/2024 | |
5.0.0-beta.2 | 115,566 | 2/15/2024 | |
5.0.0-beta.1 | 143,590 | 6/29/2023 | |
4.6.0-beta.1 | 544 | 5/2/2023 | |
4.5.0 | 1,759,447 | 4/9/2023 | |
4.4.0 | 282,652 | 2/12/2023 | |
4.3.0 | 695,171 | 11/10/2022 | |
4.2.1 | 169,060 | 9/18/2022 | |
4.2.0 | 37,313 | 9/1/2022 | |
4.1.0 | 117,147 | 7/13/2022 | |
4.0.0 | 73,181 | 6/6/2022 | |
4.0.0-beta.5 | 5,704 | 5/26/2022 | |
4.0.0-beta.4 | 780 | 5/20/2022 | |
4.0.0-beta.3 | 113,627 | 2/22/2022 | |
4.0.0-beta.2 | 482 | 2/16/2022 | |
4.0.0-beta.1 | 7,325 | 2/9/2022 | |
3.0.2 | 1,137,658 | 10/11/2021 | |
3.0.1 | 136,713 | 10/6/2021 | |
3.0.0 | 425,265 | 5/14/2021 | |
2.6.1 | 579,500 | 10/12/2020 | |
2.6.0 | 6,267 | 10/11/2020 | |
2.5.5 | 225,397 | 6/25/2020 | |
2.5.4 | 242,133 | 5/6/2020 | |
2.5.3 | 27,449 | 4/8/2020 | |
2.5.2 | 72,461 | 3/5/2020 | |
2.5.1 | 55,595 | 2/18/2020 | |
2.5.0 | 79,096 | 1/14/2020 | |
2.4.8 | 265,971 | 10/3/2019 | |
2.4.7 | 30,078 | 8/27/2019 | |
2.4.6 | 84,206 | 5/23/2019 | |
2.4.5 | 262,677 | 11/29/2018 | |
2.4.4 | 17,995 | 10/30/2018 | |
2.4.3 | 71,315 | 10/9/2018 | |
2.3.3 | 67,038 | 8/21/2018 | |
2.3.2 | 10,316 | 8/14/2018 | |
2.3.1 | 72,831 | 6/21/2018 | |
2.3.0 | 37,126 | 5/12/2018 | |
2.2.5 | 48,703 | 3/27/2018 | |
2.2.4 | 26,976 | 3/10/2018 | |
2.2.3 | 6,080 | 3/5/2018 | |
2.2.2 | 43,834 | 2/25/2018 | |
2.2.1 | 181,125 | 12/27/2017 | |
2.2.0 | 85,175 | 11/26/2017 | |
2.1.2 | 10,438 | 11/23/2017 | |
2.1.1 | 7,861 | 11/11/2017 | |
2.1.0 | 6,287 | 11/9/2017 | |
2.0.21 | 6,746 | 11/7/2017 | |
2.0.20 | 7,036 | 11/1/2017 | |
2.0.19 | 14,820 | 10/19/2017 | |
2.0.18 | 5,817 | 10/18/2017 | |
2.0.17 | 20,172 | 9/6/2017 | |
2.0.16-beta | 5,186 | 9/5/2017 | |
2.0.15-beta | 807 | 9/2/2017 | |
2.0.14-beta | 857 | 8/30/2017 | |
2.0.13-beta | 813 | 8/29/2017 | |
2.0.12-beta | 1,310 | 8/28/2017 | |
2.0.11-beta | 1,037 | 8/21/2017 | |
2.0.10-beta | 810 | 8/20/2017 | |
2.0.9-beta | 779 | 8/20/2017 | |
2.0.8-beta | 768 | 8/20/2017 | |
2.0.7-beta | 1,391 | 8/2/2017 | |
2.0.6-beta | 968 | 7/30/2017 | |
2.0.5-beta | 32,718 | 7/21/2017 | |
2.0.4-beta | 1,234 | 7/16/2017 | |
2.0.3-beta | 959 | 7/15/2017 | |
2.0.2-beta | 3,223 | 7/11/2017 | |
2.0.1-beta | 1,008 | 7/4/2017 | |
2.0.0-beta | 1,039 | 7/2/2017 | |
1.3.2 | 50,311 | 7/13/2017 | |
1.3.1 | 35,045 | 6/25/2017 | |
1.3.0 | 63,707 | 1/21/2017 | |
1.3.0-beta | 838 | 1/20/2017 | |
1.2.0 | 6,660 | 11/9/2016 | |
1.1.4 | 141,892 | 5/30/2016 | |
1.1.3 | 1,541 | 5/11/2016 | |
1.1.2 | 6,037 | 5/2/2016 | |
1.1.1 | 12,980 | 1/10/2016 | |
1.1.0 | 1,182 | 1/10/2016 | |
1.0.11 | 4,753 | 10/5/2015 | |
1.0.10 | 1,625 | 9/21/2015 | |
1.0.9 | 1,554 | 9/3/2015 | |
1.0.8 | 3,360 | 8/1/2015 | |
1.0.7 | 108,540 | 7/16/2015 | |
1.0.6 | 1,396 | 7/10/2015 | |
1.0.5 | 2,770 | 6/27/2015 | |
1.0.4 | 1,175 | 6/24/2015 | |
1.0.3 | 1,730 | 6/4/2015 | |
1.0.2 | 1,206 | 6/2/2015 | |
1.0.1 | 1,261 | 5/30/2015 | |
1.0.0 | 12,971 | 4/20/2015 | |
1.0.0-beta | 1,123 | 4/19/2015 | |
0.1.11-beta | 1,008 | 4/13/2015 | |
0.1.10-beta | 957 | 4/13/2015 | |
0.1.9-beta | 1,123 | 4/2/2015 | |
0.1.8-beta | 903 | 4/1/2015 | |
0.1.7-beta | 24,413 | 12/7/2014 | |
0.1.6-beta | 1,096 | 12/2/2014 | |
0.1.5-beta | 1,388 | 11/13/2014 | |
0.1.4-beta | 1,128 | 10/5/2014 | |
0.1.3-beta | 1,613 | 9/25/2014 | |
0.1.2-beta | 941 | 9/11/2014 | |
0.1.1-beta | 940 | 9/10/2014 | |
0.1.0-beta | 928 | 8/29/2014 |
v4.3.0