C# 如何对 Windows 服务进行单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I unit test a Windows Service?
提问by dance2die
.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,它具有可覆盖的方法,
- OnStart
- 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 服务。
采纳答案by Guy Starbuck
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.
我可能会建议设计您的应用程序,以便 Windows 服务中的“OnStart”和“OnStop”覆盖仅调用类库程序集上的方法。这样您就可以针对类库方法自动进行单元测试,并且该设计还从 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”方法本身就是一个集成测试,而不是自动化。
回答by David Basarab
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.
我还主要通过控制台应用程序测试服务,模拟服务会做什么。这样我的单元测试就完全自动化了。
回答by Kevin Berridge
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.
这种方法的好处是您的服务非常简单。基本上它所做的就是调用方法在正确的时间做正确的工作。您的另一个程序集包含您的服务打算完成的所有工作。这使得测试非常容易,并且可以根据需要轻松重用或修改。
回答by Erik van Brakel
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.
我会使用 Windows 服务类(您在启动/停止服务时运行的那个)有点像您真实系统的代理。我看不出您的服务背后的代码与任何其他编程有何不同。onStart 和 onStop 方法只是被触发的事件,就像按下 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 ;-)
所以你的windows服务类是一个非常瘦的类,相当于一个windows窗体。它调用您的业务逻辑/域逻辑,然后执行它应该做的事情。您所要做的就是确保您在 onStart 和 onStop 中调用的方法按预期工作。至少这就是我会做的;-)
回答by Trupti
Test Window service in automatic power off, shut down conditions Test window service when network disconnected, connected Test window service option autostart, manual etc
测试窗口服务在自动断电、关闭条件下测试窗口服务在网络断开、连接时测试窗口服务选项自动启动、手动等
回答by Simone
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 documentationbut, since they are protected, you'll need to use Reflection.
无论如何,如果您真的想要,您可以在单元测试中调用这两种方法,如MSDN 文档所述,但是,由于它们受到保护,您需要使用反射。
回答by BitMask777
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.
为测试而设计是一个很好的策略,正如许多答案所指出的那样,建议将您的OnStart
和OnStop
方法委托给域对象来保持非常精简。
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[] {}});