With Integration tests a common scenario comes into picture. Integration tests always depend on system components e.g. Database, Services, etc. If the Integration test is failing unexpectedly then it might require to debug the test including that component which are usually part of the same VS solution. Recently I came across this SO question. Question seemed legit because while debugging a test, attaching a process again and again to debug becomes really annoying.
To make dependent components like services, I usually invoke the process to launch e.g. ConsoleApplication project in same solution. Its like simply add a helper class to invoke the process.
- internal class ProcessInvoker
- {
- /// <summary>
- /// Invokes the host process for test service
- /// </summary>
- public static void InvokeDummyService()
- {
- var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- ProcessStartInfo info = newProcessStartInfo(Path.Combine(path, "DummyService.exe"));
- info.UseShellExecute = true;
- info.WorkingDirectory = path;
- var process = Process.Start(info);
- // This is what would attach the process to current VS debugger
- AttachDebugger.ToProcess(process.Id);
- }
- /// <summary>
- /// Kills the process of service host
- /// </summary>
- public static void KillDummyService()
- {
- Process.GetProcessesByName("DummyService").ToList().ForEach(x => x.Kill());
- }
- }
Now in the TestInitialize and TestCleanup methods (I’m using NUnit in this sample code so it would be TestFixtureSetup and TextFixtureTearDown methods) I would start the process and kill the respective process. Here the Integration test is a sample from one of my Github project WCFDynamicProxy. In this Test I’ll be invoking a Dummy service instance hosted in a Console application. Then completion of test just kill the process.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using SampleService.Contracts;
- using WcfDynamicProxy.Tests.Helper;
- using NUnit.Framework;
- namespace WcfDynamicProxy.Tests.Tests
- {
- /// <summary>
- /// Implements the tests for CustomClientProxyFactory class. The tests have falvour of integration
- /// as we're testing it on dummy service client.
- /// </summary>
- [TestFixture]
- public class CustomClientProxyFactoryTest
- {
- /// <summary>
- /// Setup required before the tests of the fixture will run.
- /// </summary>
- [TestFixtureSetUp]
- public void Init()
- {
- ServiceHostProcessInvoker.InvokeDummyService();
- }
- /// <summary>
- /// Tear down to perform clean when the execution is finished.
- /// </summary>
- [TestFixtureTearDown]
- public void TearDown()
- {
- ServiceHostProcessInvoker.KillDummyService();
- }
- /// <summary>
- /// Case when the method GetDefaultErrorLoggingInterfaceProxy was invoked to get the real
- /// proxy of a test service, succeeds.
- /// </summary>
- [Test]
- public void GetDefaultWCFClientProxy_RequestingProxyClientGeneration_Succeeds()
- {
- var proxy = DynamicProxyWrapperFactory.GetProxyInstanceByInterface < ITestService > ("TestService");
- var result = proxy.SayHello();
- Assert.IsNotNull(result, "The result was null.");
- Assert.IsNotEmpty(result, "The result was empty.");
- }
- }
- }


Amit ChoudharyPosted Feb 3, 2016, 2:05 PM
Thank you everyone.
Ankit SaxenaPosted Feb 3, 2016, 12:14 PM
Thanks for sharing. Usefull info.
Shubham KumarPosted Feb 2, 2016, 1:53 AM
thnx sir
Mohammed IbrahimPosted Feb 1, 2016, 2:10 PM
nice
Santhakumar MunuswamyPosted Feb 1, 2016, 1:54 PM
Thanks for nice article
Muhammad Aqib ShehzadPosted Feb 1, 2016, 5:17 AM
good one
Sthitaprajnya Debasis NayakPosted Feb 1, 2016, 2:16 AM
Nice !!!
Sibeesh VenuPosted Feb 1, 2016, 1:09 AM
Nice Share
Ankit BansalPosted Feb 1, 2016, 12:41 AM
nice one..