3

我第一次尝试实现测试驱动的开发。我的项目是 dotnet 3.5 中的 ac#。我已经阅读了 C# 中的专业测试驱动开发一书,现在我想测试包含 Windows 服务的项目。我读过最佳实践是所有代码都必须经过测试。以下是我的 Windows 服务实现方法 onStart 和 onStop

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using log4net;

namespace MyUcmaService
{
 public partial class MyUcmaService : ServiceBase
 {
    private Worker _workerObject;
    private static MyUcmaService aMyUcmaService;
    private Thread _workerThread;
    private static ILog _log = LogManager.GetLogger(typeof(MyUcmaService));

    public MyUcmaService()
    {
        InitializeComponent();
        aMyUcmaService = this;
    }

    protected override void OnStart(string[] args)
    {
        // TODO: inserire qui il codice necessario per avviare il servizio.
        //Debugger.Launch();
        AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
        try
        {
            _workerObject = new Worker();
            _workerThread = new Thread(_workerObject.DoWork);

            // Start the worker thread.
            _workerThread.Start();

        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

    protected override void OnStop()
    {
        // TODO: inserire qui il codice delle procedure di chiusura necessarie per arrestare il servizio.
        try
        {
            _workerObject.RequestStop();
            _workerThread.Join();
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }


    private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        HandleException(e.ExceptionObject as Exception);
    }

    private static void HandleException(Exception ex)
    {
        if (ex == null)
            return;
        _log.Error(ex);

        if (aMyUcmaService != null)
        {
            aMyUcmaService.OnStop();
        }

      }
    }
   }

你能告诉我如何在这里实现 tdd 吗?感谢您的回复。

4

2 回答 2

6

您不会对服务进行 TDD,而是对服务将用于完成其工作的对象进行 TDD。

这有几个优点

  • 您的对象从一开始就与服务、GUI 或其他对象使用无关。
  • 在任何单元测试框架中创建、测试和销毁普通对象比启动、测试和停止服务要容易和快速得多。

底线

  • TDD 你自己的代码,并把尽可能多的第三方设置排除在外(TDD'ing 其他人的代码首先是矛盾的:))
  • 让服务使用您的对象。
  • 自动化您的测试用例。
于 2013-02-18T10:13:39.943 回答
4

因为我刚刚在工作中重构了 4 个现有的 Windows 服务,所以我忍不住要提供一个额外的答案!

我所做的是完全剥离 Windows 服务类,并ServiceBase为 4 种不同的实现创建我自己的 4 派生类。最根本的原因是,由于其不方便的测试周期,测试您的 windows 服务真的很痛苦:

应用更改、构建、卸载 windows 服务、安装更新的 windows 服务、测试、调试和重复...

对我来说,对 Windows 服务进行 TDD 的主要目的是:

  • 解决所有死锁问题。
  • 验证是否调用了对其他对象的委托调用。
  • 大幅缩短开发-测试周期以加快开发速度!

我从您的代码示例中认识到同样的需求。请允许我展示我自己的简化代码来描绘您可以做些什么来成功 TDD 您的 Windows 服务。

我将首先展示测试,因为这是有趣的部分。我将在测试下方添加一些已实现类的片段作为参考。

我的 [SetUp] 和单元测试

在真正的东西开始之前的一些设置东西......

    private MockRepository _mocks;
    private IAdminLayer _adminLayer;
    private IAlertSchedule _alertingServices;
    private IAlertManager _alertingManager;
    private AutoResetEvent _isExecutedSuccesful;
    private AdministratorAlertingService _alertingService;

    [SetUp]
    public void Setup()
    {
        _isExecutedSuccesful = new AutoResetEvent(false);

        _mocks = new MockRepository();
        _adminLayer = _mocks.DynamicMock<IAdminLayer>();
        _alertingServices = _mocks.DynamicMock<IAlertSchedule>();
        _alertingManager = _mocks.DynamicMock<IAlertManager>();
        var settings = _mocks.DynamicMock<ISettingsService>();

        using (_mocks.Record())
        {
            Expect.Call(_adminLayer.LogSource).Return("myLogSource").Repeat.Any();
            Expect.Call(_adminLayer.Settings).Return(settings);
            Expect.Call(settings.IsInitialised()).Return(true);
            Expect.Call(settings.GetAlertSchedule()).Return(_alertingServices);
        }
        _alertingService = new AdministratorAlertingService(_adminLayer, null);
    }

测试OnStart行为:

    [Test]
    public void AlertingServiceTestOnStart()
    {
        new Thread(ExecuteOnStart).Start();
        Assert.IsTrue(_isExecutedSuccesful.WaitOne());
        Assert.IsTrue(_alertingService.ServiceTimer.Enabled);
    }

    private void ExecuteOnStart()
    {
        _alertingService.OnStart();
        _isExecutedSuccesful.Set();
    }

测试OnPause行为:

    [Test]
    public void AlertingServiceTestOnPause()
    {
        new Thread(ExecuteOnPause).Start();
        Assert.IsTrue(_isExecutedSuccesful.WaitOne());
        Assert.IsFalse(_alertingService.ServiceTimer.Enabled);
    }

    private void ExecuteOnPause()
    {
        _alertingService.OnPause();
        _isExecutedSuccesful.Set();
    }

我对服务功能的实现

一段有趣且最有意义的部分:

public abstract class AdministratorServiceBase
{
    protected readonly IAdminLayer AdminLayer;
    protected readonly ServiceBase Service;
    public Timer ServiceTimer = new Timer();      
    protected AutoResetEvent ResetEvent = new AutoResetEvent(true);

    protected AdministratorServiceBase(IAdminLayer adminLayer, ServiceBase service, string name, string logname, string logsource, string version)
    {
        // Removed irrelevant implementation
        ServiceTimer.Elapsed += ServiceTimerElapsed;
    }

    public virtual void OnStart()
    {
        try { // Removed irrelevant implementation }
        catch (Exception ex)
        {
            HandleException(" detected a failure (trying to start).", ex, true, true);
        }            
    }

    // Same story for the other service methods...
    public virtual void OnPause() {}
    public virtual void OnContinue() {}
    // ..
    // ..
}

你如何在真正的 WindowsService 类中使用你的服务类

(这是视觉基础,但这不会有太大的不同)

Public Class Service1

    Private ReadOnly _alertingService As AdministratorAlertingService = New AdministratorAlertingService(AdminLayer.GetSingleInstance(), Me)

    Protected Overrides Sub OnStart(ByVal args() As String)
        _alertingService.OnStart()
    End Sub

    Protected Overrides Sub OnPause()
        _alertingService.OnPause()
    End Sub

    // etc etc 

End Class

两天重构了4个windows服务,收益不可估量!TDD 确实帮助我提供了质量。


回复您的评论

我的 Windows 服务类是Service1视觉基础类。它创建一个 AdministratorAlertingService.

Private ReadOnly _alertingService As AdministratorAlertingService = 
    New AdministratorAlertingService(/* parameters /*)

AdministratorAlertingService扩展了AdministratorServiceBaseClass包含我的其他 Windows 服务也具有的共享行为(计时器、启动、暂停、停止)。

如果您只有一个 Windows 服务,那么您当然不需要基类。

在我的单元测试中,我创建了一个新的 SuT(被测对象),在这种情况下它是一个新的AdministratorAlertingService,我使用AutoResetEvent. Windows 服务完成的“实际工作”在专门针对这些类的单元测试中进行了模拟和测试。

这样您就可以(并且应该)对 Windows 服务进行 TDD。它将大大减少您的 Windows 服务的开发测试周期。

您可以选择将集成测试添加到您的测试套件以测试完整的功能:您委托的手写启动、暂停、停止行为,您不模拟执行实际工作的类的功能。我在我的 AdministratorServices 中获得了最多的 TDD。


我希望它有帮助!享受您的 TDD 冒险。

于 2013-02-18T18:16:00.293 回答