How can I unit test a Windows Service?

前端 未结 7 1695
青春惊慌失措
青春惊慌失措 2020-12-08 13:03

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

7条回答
  •  一个人的身影
    2020-12-08 13:42

    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.

提交回复
热议问题