简体   繁体   English

如何对Windows服务进行单元测试?

[英]How can I unit test a Windows Service?

.NET Framework: 2.0 Preferred Language: C# .NET Framework:2.0首选语言:C#

I am new to TDD (Test Driven Development). 我是TDD(测试驱动开发)的新手。

First of all, is it even possible to unit test Windows Service? 首先,甚至可以对Windows服务进行单元测试吗?

Windows service class is derived from ServiceBase, which has overridable methods, Windows服务类派生自ServiceBase,它具有可覆盖的方法,

  1. OnStart 的OnStart
  2. OnStop 调用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. 我查看了WCF服务问题,但由于我从未处理过WCF服务,因此对我没有任何意义。

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. 我可能建议您设计应用程序,以便Windows服务中的“OnStart”和“OnStop”覆盖只调用类库程序集上的方法。 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. 这样,您可以针对类库方法自动执行单元测试,并且该设计还可以从Windows服务的实现中抽象出您的业务逻辑。

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. 在这种情况下,在Windows服务上下文中测试“OnStart”和“OnStop”方法本身将是一个集成测试,而不是您要自动化的东西。

I have unit tested windows services by not testing the service directly, but rather testing what the service does. 我通过不直接测试服务来测试Windows服务,而是测试服务的功能。

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. 这使得测试非常容易,并且可以根据需要轻松重用或修改。

I would start here . 我会从这里开始。 It shows how to start and stop services in C# 它展示了如何在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. 这样我的单元测试就完全自动化了。

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. 我会使用Windows服务类(当你启动/停止服务时运行的那个)有点像你真实系统的代理。 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. onStart和onStop方法只是被触发的事件,就像按下GUI上的按钮一样。

So your windows service class is a very thin class, comparable to a windows form. 因此,您的Windows服务类是一个非常薄的类,可与Windows窗体相媲美。 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. 您所要做的就是确保您在onStart和onStop上调用的方法正如他们应该的那样工作。 At least that's what I would do ;-) 至少那是我会做的;-)

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. 设计测试是一个很好的策略,因为许多答案通过委派给域对象来推荐OnStartOnStop方法保持非常精简。

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): 但是,如果您的测试由于某种原因需要执行服务方法,您可以使用这样的代码在测试方法中调用它们(在此示例中调用OnStart ):

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

自动断电,关机条件下的测试窗口服务网络断开时连接测试窗口服务,测试窗口服务选项自动启动,手动等

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. 无论如何,如果你真的想,你可以在单元测试中调用MSDN文档所描述的这两种方法,但是,由于它们受到保护,你需要使用Reflection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM