Running a WCF service before unit testing
I have a VS 2010 solution that contains two projects: the first is a WCF service, and the second is a unit testing project, holding a reference to the service and testing the methods the service is exposing. The unit test project is developed using the Microsoft.VisualStudio.TestTools.UnitTesting framework.
Before running the testing project I run locally the WCF service using right clock on the SVC file and choose the option "View in Browser". Only when the service is "up in the air" the tests can actually run.
My question is whether there is an option to run the service automatically before the unit tests start running, using a C# code or some kind of a script?
If you truly want to operate on hosted service, you can self-host it within unit test. Then it will be independent from infrastructure, making it repeatable, so future developers will have ability to run them without making any additional setups.
But think twice, do you want to test service logic or its hosting infrastructure? WCF has decoupled implementation by using contracts (interfaces). You can successfully test service logic by testing service class as any other class, calling web service methods as any other methods.
If your service has an external dependencies (like other services), they should be maintained by Dependency Injection. It will allow you to mock them. If you service relies on global, or static resources (like OperationContext), create a wrapper, that will allow you to inject its mock also. Or use more sophisticated solution like Microsoft Fakes (refer also this tutorial) or Typemock Isolator.
When you will have all logic tested, then you can proceed with integration test with IIS hosted service, testing connectivity to other resources and boundary conditions like timeouts etc.
Install or configure IIS and run the WCF service from a URL on localhost
Run your tests from this.
Although, in the strictest sense of the word, running tests on your code via http is an integration test and not a unit test.