Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS hosted WCF service: Integration tests and code coverage

For a project I have programmed a wcf service library. It can be hosted in IIS and in a self-hosted service.

For all external systems that are connected, I have provided Mock implementations which give some generic data, so such the service (library) keeps running and doing work. It is a classic automaton / finite-state machine.

While bootstrapping, all data sources are connected. In testing mode, the mock implementations are connected. So when I run tests, the service library is "started" from a self-hosted service, not IIS and the the state machine keeps running and processing data packages.

Is there any way to get some kind of "test coverage" from such a run.

I would really appreciate if I could tell which code paths are hit by the example data I provide from the mock objects. And then provide more testdata to get a higher coverage.

If I could do this without having to provide "lots of extra" testing code, it would be great. I think a lot of cases are already covered from the data provided from the mock objects. But right now I have no starting point for that.

Here are some code examples to give a more clear picture of what is meant. Code is strongly simplified of course.

In a very simple console application to start the service (self hosted version)

static void Main(string[] args)
{
    using (var host = new ServiceHost(typeof(MyServiceLib.Service.MyServiceLib)))
    {
        host.Open();
        Console.ReadLine();
        host.Close();
    }
}

In the service library, a constructor is called from that code

public MyServiceLib()
{
    Task.Factory.StartNew(this.Scaffold);
}

Which does nothing more than starting the state machine

private void Scaffold()
{
    // lots of code deleted for simplicity reasons
    var dataSource = new MockDataSource();

    // inject the mocked datasource
    this.dataManager = new DataManager(dataSource);

    // this runs in its own thread. There are parts that are started on a timer event.
    this.dataManager.Start();
}

public class DataManager : IDataManager
{
     public void Start()
     {
         while (this.IsRunning)
         {
             var data = this.dataSource.getNext();

             if (data != null)
             {
                 // do some work with the data retrieved
                 // lots of code paths will be hit from that
                 this.Process(data);
             }
             else
             {
                 Thread.Sleep(1000);
             }
         }
     }

     public void Process(IData data)
     {
        switch (data.PackageType)
        {
            case EnumPackageType.Single:
            {
                ProcessSingle(data);
                break;
            }
            case EnumPackageType.Multiple:
            {
                ProcessMultiple(data);
                break;
            }
            // here are lots of cases
            default:
            {
                Logger.Error("unknown package type");
                break;
            }
        }
     }
}

What I have tried so far:

  1. OpenCover

with a special test dll that would create the Host as shown above, but the host cannot be created properly, so the testing does not start really. I get a "Host is in fault state" error message. I followed this mini-tutorial. Despite that I get a coverage report with a calculated coverage of about 20%. But the service is just starting, it is not doing any work so far.

  1. Visual Studio Performance Tools

The steps are essentially described in this article. I get a myproject.coverage file, but I cannot view it, because I only have a VS Professional, the coverage seems to be only of use in Test Premium or Ultimate editions.

Besides having tried those two, I will accept any answer showing how to get it up and running with any of those (openCover preferred).

Will accept an answer that shows how to test this setup and get a code coverage while leveraging tools to generate most of the code (as pex would, but after trial I see it does not generate very good code).

like image 868
Mare Infinitus Avatar asked Jan 31 '15 18:01

Mare Infinitus


People also ask

Do integration tests count for code coverage?

As a Developer or Tester, you can absolutely include Integration Testing in Code Coverage, if you have integrated code scripts.


1 Answers

It would help to see the operations of the service.

I never tried running such "console kind" application under a coverage tool.

I would suggest writing a test with let's say NUnit (or any other unit testing framework; it's not a unit test, obviously, but the technique fits quite well).

In the test, you open the service host, create a client of the service, let the client execute some operations on your service, and close the service host.

Run this test under a coverage tool, and you should be done.

I've done that with NUnit and NCover about 7 years ago, using their current versions at that time (NCover was free software, if I remember it right).

like image 100
felix-b Avatar answered Sep 28 '22 09:09

felix-b