<p>.NET Framework: 2.0
Preferred Language: C#</p>

<p>I am new to TDD (Test Driven Development).</p>

<p>First of all, is it even possible to unit test Windows Service?</p>

<p>Windows service class is derived from ServiceBase, which has overridable methods, </p>

<ol>
<li>OnStart </li>
<li>OnStop</li>
</ol>

<p>How can I trigger those methods to be called as if unit test is an actual service that calls those methods in proper order?</p>

<p>At this point, am I even doing a Unit testing? or an Integration test?</p>

<p>I have looked at WCF service question but it didn't make any sense to me since I have never dealt with WCF service.</p>

[{'answer_id': 42155, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 3, 'selected': False, 'text': '<p>I would start <a href="http://www.csharp-examples.net/restart-windows-service/" rel="nofollow noreferrer">here</a>.  It shows how to start and stop services in C#</p>\n\n<p>A sample to start is is</p>\n\n<pre><code>public static void StartService(string serviceName, int timeoutMilliseconds)\n{\n  ServiceController service = new ServiceController(serviceName);\n  try\n  {\n    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);\n\n    service.Start();\n    service.WaitForStatus(ServiceControllerStatus.Running, timeout);\n  }\n  catch\n  {\n    // ...\n  }\n}\n</code></pre>\n\n<p>I have also tested services mostly through console app, simulating what the service would do.  That way my unit test is completely automated.</p>\n'}, {'answer_id': 42157, 'author': 'Guy Starbuck', 'author_id': 2194, 'author_profile': 'https://Stackoverflow.com/users/2194', 'pm_score': 8, 'selected': True, 'text': '<p>I\'d probably recommend designing your app so the "OnStart" and "OnStop" overrides in the Windows Service just call methods on a class library assembly.  That way you can automate unit tests against the class library methods, and the design also abstracts your business logic from the implementation of a Windows Service.</p>\n\n<p>In this scenario, testing the "OnStart" and "OnStop" methods themselves in a Windows Service context would then be an integration test, not something you would automate.</p>\n'}, {'answer_id': 42161, 'author': 'Kevin Berridge', 'author_id': 4407, 'author_profile': 'https://Stackoverflow.com/users/4407', 'pm_score': 5, 'selected': False, 'text': '<p>I have unit tested windows services by not testing the service directly, but rather testing what the service does.</p>\n\n<p>Typically I create one assembly for the service and another for what the service does.  Then I write unit tests against the second assembly.  </p>\n\n<p>The nice thing about this approach is that your service is very thin.  Basically all it does is call methods to do the right work at the right time.  Your other assembly contains all the meat of the work your service intends to do.  This makes it very easy to test and easy to reuse or modify as needed.</p>\n'}, {'answer_id': 42165, 'author': 'Erik van Brakel', 'author_id': 909, 'author_profile': 'https://Stackoverflow.com/users/909', 'pm_score': 3, 'selected': False, 'text': "<p>I would use the windows service class (the one you run when you start/stop the service) sort of like a proxy to your real system. I don't see how the code behind your service should be any different from any other programming. The onStart and onStop methods are simply events being fired, like pushing a button on a GUI.</p>\n\n<p>So your windows service class is a very thin class, comparable to a windows form. It calls your business logic/domain logic, which then does what it's supposed to do. All you have to do is make sure the method(s) you're calling in your onStart and onStop are working like they're supposed to. At least that's what I would do ;-)</p>\n"}, {'answer_id': 2543421, 'author': 'Trupti', 'author_id': 304859, 'author_profile': 'https://Stackoverflow.com/users/304859', 'pm_score': 0, 'selected': False, 'text': '<p>Test Window service in automatic power off, shut down conditions\nTest window service when network disconnected, connected\nTest window service option autostart, manual etc</p>\n'}, {'answer_id': 6042190, 'author': 'Simone', 'author_id': 347137, 'author_profile': 'https://Stackoverflow.com/users/347137', 'pm_score': -1, 'selected': False, 'text': '<p>Guy\'s probably the best answer.</p>\n\n<p>Anyway, if you really want to, you could just invoke in the unit test these two method as described by <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase%28v=VS.90%29.aspx" rel="nofollow">MSDN documentation</a> but, since they are protected, you\'ll need to use Reflection.</p>\n'}, {'answer_id': 33901272, 'author': 'BitMask777', 'author_id': 509891, 'author_profile': 'https://Stackoverflow.com/users/509891', 'pm_score': 2, 'selected': False, 'text': '<p>Designing for test is a good strategy, as many of the answers point out by recommending that your <code>OnStart</code> and <code>OnStop</code> methods stay very thin by delegating to domain objects. </p>\n\n<p>However, if your tests do need to execute the service methods for some reason, you can use code like this to call them from within a test method (calling <code>OnStart</code> in this example):</p>\n\n<pre><code>serviceInstance.GetType().InvokeMember("OnStart", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, serviceInstance, new object[] {new string[] {}});\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/42150', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4035/']