Click here to Skip to main content
13,868,845 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.3K views
1 bookmarked
Posted 2 Apr 2017
Licenced CPOL

Consumer Driven Contract Testing with PactNet C#

, 2 Apr 2017
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Adi_Mag
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionWhere are the graphics!? Pin
Carlos19073-Apr-17 1:31
professionalCarlos19073-Apr-17 1: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 3 Apr 2017
Article Copyright 2017 by Adi_Mag
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid