我如何单元测试Windows服务?单元测试、Windows

2023-09-02 10:30:32 作者:酒太甜你太苦

.NET框架:2.0 preferred语言:C#

.NET Framework: 2.0 Preferred Language: C#

我是新来的TDD(测试驱动开发)。

I am new to TDD (Test Driven Development).

首先,是它甚至有可能进行单元测试Windows服务?

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

Windows服务类是从ServiceBase的,其中有重写的方法,得出

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

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

我已经看过了WCF服务的问题,但它并没有任何意义,我,因为我从来没有处理WCF服务。

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

推荐答案

我可能会建议您设计的应用程序使的OnStart和的onStop在Windows服务覆盖只是调用一个类库装配方法。这样,您就可以自动单元对类库的方法测试,而且设计也抽象你的业务逻辑从Windows服务的实施。

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.

在这种情况下,测试的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.

 
精彩推荐
图片推荐