C# – define WCF client endpoints in a test project

cintegration-testingnetvisual studiowcf

I have a class library– call it Services.dll— which is a wrapper for some third party functionality. The third party gave us a bunch of DLLs and those "inner" DLLs call a WCF service.

Normally, when Services.dll is running, it lives within the web site's process, and uses the web.config associated with the site. So when I want to define the WCF client endpoints, I edit web.config. This is very straightforward.

But now I want to write an automated integration test suite. The test project is of course a class library (let's call it Services.Tests.dll), so it will take whatever config is defined for the hosting process. But I think in the case of a test project, the hosting process is Visual Studio itself. The test project certainly does not emit an .exe of its own.

So where do I define the WCF client endpoints for the test project? Which config file do I edit? Do I edit devenv.exe.config? Do I set up some kind of "satellite" config file (e.g. Services.Tests.dll.config)– if so, how do I get the Visual Studio test runner to pick up the config?

Or… is there a way to do this without config, by intercepting/mocking calls to System.Configuration? I can't unfortunately change the third party DLLs.

Or.. do I override with some clever use of ServicePointManager, so that config is not an issue? Not sure how I would do that.

Or… is the best practice to write an integration test project not as a class library but as an .exe, with its own app.config?

Please note– I am not running unit tests (those are separate). These are integration tests, so I don't want to isolate away the WCF calls. I need them, and I need them to be configured to point at the right test servers.

Best Answer

There is a simple trick you can use.

Visual Studio will load the app.config for the test project into it's host process if it's present. However, test projects don't usually have app.configs. You can force visual studio to create one by adding settings to your project:

  1. Right click your project and select "properties".
  2. Go to the "Settings" option, on the left-side bar.
  3. Insert anything on this page. Just anything will work.
  4. Save the project.

Now, you'll see that your test project has now a app.config file. Just add your WCF endpoints/bindings there and you're good.

Related Topic