How can I unit test a Windows Service?

C#.NetUnit TestingWindows Services

C# Problem Overview


.NET Framework: 2.0 Preferred Language: C#

I am new to TDD (Test Driven Development).

First of all, is it even possible to unit test Windows Service?

Windows service class is derived from ServiceBase, which has overridable methods,

  1. OnStart
  2. OnStop

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

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

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

C# Solutions


Solution 1 - C#

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.

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.

Solution 2 - C#

I have unit tested windows services by not testing the service directly, but rather testing what the service does.

Typically I create one assembly for the service and another for what the service does. Then I write unit tests against the second assembly.

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.

Solution 3 - C#

I would start here. It shows how to start and stop services in C#

A sample to start is is

public static void StartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

I have also tested services mostly through console app, simulating what the service would do. That way my unit test is completely automated.

Solution 4 - C#

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.

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 ;-)

Solution 5 - C#

Designing for test is a good strategy, as many of the answers point out by recommending that your OnStart and OnStop methods stay very thin by delegating to domain objects.

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 OnStart in this example):

serviceInstance.GetType().InvokeMember("OnStart", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, serviceInstance, new object[] {new string[] {}});

Solution 6 - C#

Test Window service in automatic power off, shut down conditions Test window service when network disconnected, connected Test window service option autostart, manual etc

Solution 7 - C#

Guy's probably the best answer.

Anyway, if you really want to, you could just invoke in the unit test these two method as described by MSDN documentation but, since they are protected, you'll need to use Reflection.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondance2dieView Question on Stackoverflow
Solution 1 - C#Guy StarbuckView Answer on Stackoverflow
Solution 2 - C#Kevin BerridgeView Answer on Stackoverflow
Solution 3 - C#David BasarabView Answer on Stackoverflow
Solution 4 - C#Erik van BrakelView Answer on Stackoverflow
Solution 5 - C#BitMask777View Answer on Stackoverflow
Solution 6 - C#TruptiView Answer on Stackoverflow
Solution 7 - C#SimoneView Answer on Stackoverflow