Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C#

Consumer Driven Contract Testing with PactNet C#

Rate me:
Please Sign up or sign in to vote.
4.92/5 (5 votes)
2 Apr 2017CPOL2 min read 24.9K   1   1
Consumer driven contract testing with pact

Contract

In simple terms, it is an agreement between the service consumer (Client application) and service provider (WebApi).

Why Do We Need Consumer Driven Contract Testing?

As we are moving towards micro services architecture, the client and provider rely on contracts. Both the applications are loosely coupled. If provider modifies the contract without informing the client, then it will break the application at the client end.

Most of the times, consumer application and provider application are owned by different teams. If we want to test the contracts, then it will be a pain. To mitigate this problem, we need to involve both the teams (Consumer and Service team) for integration testing. But this process is very costly, dependency on other teams and is time consuming.

To resolve the above issue, we can use pact tool for contract testing.

Pact: It is a tool for contract testing. For dotnet, we can use “PactNet”.

How Does It Work?

It works in 2 steps. For more details, check out url (https://docs.pact.io/documentation/).

Step 1 - Consumer End

  1. Setup fake service with the help of pact.
  2. Mock request and response for the API call.
  3. Call the Client method from pact test class.
  4. Client calls the fake service and it will return the fake response.
  5. It will generate a json file with complete details of request and response.
  6. Share this json file with provider.

Step 2 - Provider End

  1. Read request and response from json files.
  2. Pact tests replay all the requests with actual API.
  3. Pact will verify all the responses.

Pact Implementation

You can download the pact application from github (https://github.com/Head-Strong/Pact.Test.git).

Solution Overview: It consist of three projects:

  1. Provider.Test: Provider tests
  2. Test.Pact.App: Consumer tests
  3. WebApi2: Service

Setup fake service at consumer end:

It will create a json file with name as “clientname”-“providername”.json.

Pact Test Scenario at Consumer Side.

After running these tests, it will generate a json file. Structure of Json file is as follows: 

Replay scenario at Provider side.

This above code reads the json file and replays all the scenarios by calling the actual service.

Benefits of Pact

  1. Removes manual testing efforts
  2. Less error prone
  3. Removes dependency of other teams
  4. Quick feedback

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhere are the graphics!? Pin
Carlos19073-Apr-17 0:31
professionalCarlos19073-Apr-17 0:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.