Pact.Provider.Wrapper
1.3.0
See the version list below for details.
dotnet add package Pact.Provider.Wrapper --version 1.3.0
NuGet\Install-Package Pact.Provider.Wrapper -Version 1.3.0
<PackageReference Include="Pact.Provider.Wrapper" Version="1.3.0" />
<PackageVersion Include="Pact.Provider.Wrapper" Version="1.3.0" />
<PackageReference Include="Pact.Provider.Wrapper" />
paket add Pact.Provider.Wrapper --version 1.3.0
#r "nuget: Pact.Provider.Wrapper, 1.3.0"
#addin nuget:?package=Pact.Provider.Wrapper&version=1.3.0
#tool nuget:?package=Pact.Provider.Wrapper&version=1.3.0
About
In my use cases of Pactnet, i needed to perform broker related tasks in different jobs in cicd, i also wanted to be able to connect PactNet to my own broker. There fore there was some code repeating across all my projects that i preferred to have them as a separate package. And here it is.
This library
- Merges all available Pact files
- Performs tests per each interaction
- Publishes the results for each Endpoint.
- Each request path, is considered an api endpoint, it may have one or more interactions.
- The result of testing an endpoint, is successful when all interaction tests towards this endpoint is passed.
- Publishers are extendable, the package also contains an Html publisher which produces an html file presenting the results.
In my case, i publish the results to a badge server and i use these badges in my api wiki, so any team member can have see the state of implementation of apis at the api wiki.
Important Note |
---|
pact-net package wraps the ruby version of pact and when using it and you need to reference its package regarding the platform your tests are running at. So for example |
in my case, which i run my tests on a 64 bit linux, i add <PackageReference Include="PactNet.Linux.x64" Version="3.0.0" /> reference alongside the <PackageReference Include="Pact.Provider.Wrapper" /> reference in my csproj file (or via package manager and etc.). Other platforms can be PactNet.Windows, PactNet.Linux.x86 and PactNet.OSX |
Examples
Without publishing the results
// Unit test method based on exceptions like xUnit (with [Fact] attribute or NUnit or etc...
public void Providers_GivenPacts_ShouldHonorAllPacts(){
// settup your application
var app = SetupApp();
// Start api service(s)
app.Start();
// Create test bench
// This example assumes your services would be up at http://localhost:9222
var bench = new PactVerificationBench("http://localhost:9222");
// Assuming the test projects executable is built at
// <project-directory>/bin/<configuration>/<sdk>/<executable>
// for example: Example.Pact.Test/bin/Debug/netcoreapp3.1/Example.Pact.Test
bench.Verify("../../../../Pacts");
// Stop api service(s)
app.StopAsync().Wait();
}
With Original Pact broker
// Unit test method based on exceptions like xUnit (with [Fact] attribute or NUnit or etc...
public void Providers_GivenPacts_ShouldHonorAllPacts(){
// settup your application
var app = SetupApp();
// Start api service(s)
app.Start();
// Create test bench
// This example assumes your services would be up at http://localhost:9222
var bench = new PactVerificationBench("http://localhost:9222");
// >>>>>>>>>>>>>>>>>>>>>>
// Use DefaultPactnetVerificationPublish which makes test bench use the
// Original Pact broker for publishing results besed on _links data.
bench.PactnetVerificationPublish = new DefaultPactnetBrokerPublish();
// Assuming the test projects executable is built at
// <project-directory>/bin/<configuration>/<sdk>/<executable>
// for example: Example.Pact.Test/bin/Debug/netcoreapp3.1/Example.Pact.Test
bench.Verify("../../../../Pacts");
// Stop api service(s)
app.StopAsync().Wait();
}
Use Html Publisher
// Unit test method based on exceptions like xUnit (with [Fact] attribute or NUnit or etc...
public void Providers_GivenPacts_ShouldHonorAllPacts(){
// settup your application
var app = SetupApp();
// Start api service(s)
app.Start();
// Create test bench
// This example assumes your services would be up at http://localhost:9222
var bench = new PactVerificationBench("http://localhost:9222");
// >>>>>>>>>>>>>>>>>>>>>>
// With this line, an html file named Report.html will be generated where
// your executable file is placed. The filename argument can point to any
// other address you prefer.
bench.WithPublishers()
.Add(new HtmlReportVerificationPublisher("Report.html"))
// Assuming the test projects executable is built at
// <project-directory>/bin/<configuration>/<sdk>/<executable>
// for example: Example.Pact.Test/bin/Debug/netcoreapp3.1/Example.Pact.Test
bench.Verify("../../../../Pacts");
// Stop api service(s)
app.StopAsync().Wait();
}
Use a Custom Publisher
You can implement the interface IVerificationPublisher
, and use it for publishing verification results.
I have my own implementation named: AcidmanicPactBrokerPublisher, which takes an address and a token in
its constructor. Where the address points to where I host my broker. my broker
Also needs a token to communicate, which is also passed to the constructor.
(In real case scenario, to prevent putting passwords in code, i take it from environment variables, which is useful when you are running your tests in cicd pipelines)
// Unit test method based on exceptions like xUnit (with [Fact] attribute or NUnit or etc...
public void Providers_GivenPacts_ShouldHonorAllPacts(){
// settup your application
var app = SetupApp();
// Start api service(s)
app.Start();
// Create test bench
// This example assumes your services would be up at http://localhost:9222
var bench = new PactVerificationBench("http://localhost:9222");
// In this example the class YourCustomPactBrokerPublisher, is an implementation
// of the interface IVerificationPublisher
bench.WithPublishers().Add(new YourCustomPactBrokerPublisher());
// Assuming the test projects executable is built at
// <project-directory>/bin/<configuration>/<sdk>/<executable>
// for example: Example.Pact.Test/bin/Debug/netcoreapp3.1/Example.Pact.Test
bench.Verify("../../../../Pacts");
// Stop api service(s)
app.StopAsync().Wait();
}
You can also use multiple Publishers together by calling the Add method in a fluent syntax fashion.
bench.WithPublishers()
.Add(new HtmlReportVerificationPublisher("Report.html"))
.Add(new YourCustomPactBrokerPublisher());
That's about it. I hope it helps saving your time.
Run Tests Separately
In version >= 1.1.0, you can add following attributes to your test method to control what 'Endpoints' are going to be considered for Contract testing:
[SkipAll]
: Will not test any endpoints. All other steps will be done but no endpoints would be tested. Its useful to check if everything is working before running tests.[Endpoint("path/to/service")]
This attribute will add an endpoint to be tested.[SkipEndpoint("path/to/service")]
: This attribute will remove given endpoint from being tested.
More Details
For More detailed version of this readme file, please refer to Github Page for this project.
Regards.
Mani.
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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 was computed. 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 (>= 2.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.6.6 | 397 | 11/30/2022 |
1.6.5 | 461 | 10/19/2022 |
1.6.4 | 580 | 9/14/2022 |
1.6.3 | 555 | 3/7/2022 |
1.6.2 | 370 | 12/12/2021 |
1.6.1 | 354 | 11/29/2021 |
1.5.8 | 351 | 11/28/2021 |
1.5.5 | 387 | 8/9/2021 |
1.5.4 | 391 | 7/24/2021 |
1.5.3 | 488 | 7/12/2021 |
1.5.2 | 428 | 7/7/2021 |
1.5.1 | 431 | 6/29/2021 |
1.5.0 | 435 | 6/29/2021 |
1.4.1 | 424 | 6/28/2021 |
1.4.0 | 373 | 6/28/2021 |
1.3.2 | 450 | 6/14/2021 |
1.3.1 | 442 | 6/13/2021 |
1.3.0 | 437 | 6/11/2021 |
1.2.0 | 404 | 6/4/2021 |
1.1.1 | 398 | 6/3/2021 |
1.1.0 | 382 | 6/2/2021 |
1.0.0 | 425 | 5/18/2021 |