views:

4484

answers:

6

.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.

+3  A: 

I woudl 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.

David Basarab
+29  A: 

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.

Guy Starbuck
+11  A: 

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.

Kevin Berridge
+2  A: 

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

Erik van Brakel
A: 

Thank you guys, I now have a general idea on how to approach to the problem.

@Kevin Berridge

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

That sentence was an eye opener for the direction I have to take. Thank you.

Sung Meister
This is not an answer. It is a comment.
Geoffrey Chetwood
A: 

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

Trupti