Skip to content

A utility for verifying producer code against all consumers on the Pact Broker

License

Notifications You must be signed in to change notification settings

Aqovia/Aqovia.PactProducerVerifier

Repository files navigation

Aqovia.PactProducerVerifier

A utility for verifying producer code against all consumers on the Pact Broker. It calls the pact broker and retrieves all pacts where it is a producer (using TeamCityProjectName config setting) and allows for branching using either the passed in Git Branch Name or the teamcity environment variable "ComponentBranch"

Build status NuGet Badge/)

Assumptions

Build server is TeamCity, as it uses the environment variable "ComponentBranch" to determine the branch of the code on the build server

Getting started

This uses the beta version of PactNet, and Team City.

  • Install the latest beta version 2.0.X-beta of PactNet package (use allow Pre-release option)

  • Install this package Aqovia.PactProducerVerifier

  • Install a test framework such as XUnit

  • Install GitInfo if you require to work out the git branch name locally

  • Add the test (example using XUnit)

    public class PactProducerTests
    {
        private readonly Aqovia.PactProducerVerifier.PactProducerTests _pactProducerTests;
        private const int TeamCityMaxBranchLength = 19;
        public PactProducerTests(ITestOutputHelper output)
        {            
			var configuration = new ProducerVerifierConfiguration
            {
                ProviderName = "<YOUR NAME OF THE PROVIDER (E.G THE PROJECT NAME)",
                PactBrokerUri = "<YOUR PACT BROKER URL>",
				PactBrokerUsername = "<YOUR PACT BROKER USERNAME OR NULL>" ,
                PactBrokerPassword = "<YOUR PACT BROKER PASSWORD OR NULL>",                
                ProjectName = "WEB API PROJECT YOU ARE TESTING (IF DOESN'T END IN Web.dll)')"
            };

			 _pactProducerTests = new PactProducerTests(configuration, output.WriteLine, ThisAssembly.Git.Branch, null, TeamCityMaxBranchLength);

			 // Or if you have given statements, set up state in the provider to match the given statements 

			 _pactProducerTests = new PactProducerTests(configuration, output.WriteLine, ThisAssembly.Git.Branch, builder =>
            {
                builder.UseMiddleware(typeof(TestStateProvider));

            }, TeamCityMaxBranchLength);
        }


        [Fact]
        public void EnsureApiHonoursPactWithConsumers()
        {
           _pactProducerTests.EnsureApiHonoursPactWithConsumers();
        }
    }

	public class TestStateProvider : BaseProviderStateMiddleware
	{
	        protected override IDictionary<string, Action> ProviderStates =>
            new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase);
			// populate with strings that match given statements with actions that set up the provider
	}

The PactProducerTests constructor takes in 3 parameters:

  • Configuration settings
  • An Action - this is used so the output of the pact test is outputted to the test results (in XUnit in this example)
  • The branch this code is in. This is used locally, but if running on the build server it uses the environment variable "ComponentBranch"
  • Callback for installing middleware in the AspNET pipeline. i.e. a custom state provider
  • The maximum branch name length (optional)

Sample

A sample is included in the source - in the samples folder. To use this:

  • Update the PactBrokerUri configuration setting to the uri of the broker your using.
  • Remove the Skip parameter in the [Fact] attribute

About

A utility for verifying producer code against all consumers on the Pact Broker

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages