Unit Testing and CI of WCF Services

This is one of the reasons why I use WCF for all of my API projects.

There are a lot of ways to test a WCF Service Application, that we are using. I would to do a disclaimer that this may be appropriate for our use but may not for your scenarios. Moreover, what we are testing are the “functionality” of the methods exposed by the service and on tests we bypass most of WCF’s infrastructure.

The first thing you have to make sure is that the service is testable. This a default WCF implementation if you create it directly from the Visual Studio project template.

image

You wanted to create the service as an abstract class and implement it as a partial class, we do this so that we split most of our logic into multiple files and for this context, which is a unit test discussion, to not to be confused about the references. As you can see, there are 353 references to the partial class and these are all from unit tests.  From this point forward, the red class is our partial class.

image

Then you add the Service Implementation project as reference to the unit test projects. Make sure to add your Message and Data Contracts as well.

image

When you create the unit tests, you call the partial classes (in red) and then use it as it was just another class. Test it, use data source if you wish.

image

Note that this bypasses all WCF infrastructure and is only used for tests like its functionality and removing the networking component. You will also notice that you are not spawning any  IIS or host the WCF service to run test against it.

image

This now can be checked-in in TFS and do automated tests in your CI.

image

Happy CI-ing WCF Services! Smile

Advertisements

Published by

johndelizo

John is currently creating digital assets that protects people and their future, a technical trainer and a full time geek specializing on development and deployment of innovations created with .NET framework on Windows platform as well as Windows infrastructure projects. He is the primary contact of the Philippine Windows Users Group, a contributor at the Microsoft Philippines Community forums and regularly conducts community sessions as well as other Microsoft technology events. John owns the Busy Saving The World Techblog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s