dimanche 8 novembre 2015

Example of Integration test

I write a sample project and then create an Integration test project for it. Please say to me,is my integration test good or not? If it's not good,please say why?.

Thanks a lot.

This is my sample class. This class get an image and send it to a windows service and get result.The windows service program resize the image and return it.

public class ImageProccessor
{
    public event DataChangedEventHandler DataChanged = null;

    protected virtual void OnDataChanged(GetRequestChanged e)
    {
        if (DataChanged != null)
            DataChanged(this, e);
    }

    private ServiceReference1.Service1Client ObjService;

    private int _IdentifyCode { get; set; }

    private System.Threading.Timer timer;

    public ImageProccessor()
    {
        ObjService = new ServiceReference1.Service1Client();
    }

    public int SendRequest(byte[] buffer)
    {
        int Result = ObjService.CreateMinImage(buffer);

        return Result;
    }

    public void GetResult(int IdentifyCode)
    {
        _IdentifyCode = IdentifyCode;

        Thread t = new Thread(() => MyThreadProc());
        t.Start();
        t.Join();
    }

    private byte[] GetRequestResponse()
    {
        byte[] FinalResult = null;

        FinalResult = ObjService.GetImage(_IdentifyCode);

        return FinalResult;
    }

    private void MyThreadProc()
    {
        // Create the timer callback delegate.
        TimerCallback cb = new TimerCallback(TimerCallBack);

        // Create the timer. It is autostart, so creating the timer will start it and execute function every 5 second.
        timer = new System.Threading.Timer(cb, null, TimeSpan.Zero, new TimeSpan(0, 0, 0, 2));
    }

    private void TimerCallBack(object state)
    {
        byte[] FinalResult = null;

        FinalResult = GetRequestResponse();

        if (FinalResult != null)
        {
            timer.Dispose();

            if (DataChanged != null)
            {
                DataChanged(this, new GetRequestChanged { Image = FinalResult });
            }
        }
    }

    public bool CheckService()
    {
        try
        {
            ObjService.CheckConnection();

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

And this is my Integration test Class with NUnit

[TestFixture]
public class ImageProccessorTest
{
    private byte[] buffer = null;

    private ImageProccessor obj;

    [SetUp]
    public void MyInit()
    {
        Assert.IsTrue(obj.CheckService(), "Service is not available");
    }

    [TestFixtureSetUp]
    public void MyTestFixtureSetUp()
    {
        try
        {
            obj = new ImageProccessor();

            buffer = File.ReadAllBytes(@"C:\Users\zarharan\Desktop\Integration Test Sample\Client\Client\Files\10.jpg");
        }
        catch
        {
            Assert.Fail("Image file for test in not exist.");
        }
    }

    [Test]
    public void SendRequestTest()
    {
        int RequestId = obj.SendRequest(buffer);

        Assert.Greater(RequestId, 0, "RequestId is not valid.");

        bool HasImage = false;

        ManualResetEvent eventRaised = new ManualResetEvent(false);

        obj.DataChanged += delegate(object sender, GetRequestChanged e)
        {
            if (e.HasImage)
            {
                eventRaised.Set();
            }
        };

        obj.GetResult(RequestId);

        Assert.That(eventRaised.WaitOne(16000), Is.True, "result is not provide after 16 second");
    }
}

Aucun commentaire:

Enregistrer un commentaire