4

I am planning to unit test WCF Services that are exposed as .svc files. What are the best practices available to unit test them.

As for as i know this is the option available:

Create a seperate project and have proxy classes created for the .svc files and add them to the project and do unit testing on those proxy classes. Proxy classes can be created using:

  1. svcutil.exe
  2. "Add Service Reference" option of visual studio.

are there any other best option available to unit test my wcf .svc file using nunit?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53
  • 1
    What exactly do you what to test? Do you what to test that all further changes are backward compatible or you need to test just logic behind services? And looks like you are looking for integration tests but not unit tests... – k0stya Nov 02 '12 at 07:21
  • I would like to test all the service logics – Alagesan Palani Nov 02 '12 at 07:24

3 Answers3

4

Basically, you don't unit test the services. You unit test their code, just as though that code were not in a service.

Don't even create an instance of the service class. That's too big a thing to unit test. You would unit test the code within that class. Remember that unit tests are meant to test the smallest possible units of your code, not the entire service.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 1
    How do you invoke code of an instance method without instantiating the class it is defined on? – Allon Guralnek Nov 02 '12 at 07:42
  • You don't instantiate the top-level class in most cases. That class uses other classes to do the job. Those are what you instantiate. – John Saunders Nov 02 '12 at 07:45
  • So you don't unit test the top-level class? What if it contains most of the logic? In many CRUD services, there is no need to delegate logic to other classes, you just write it right there. – Allon Guralnek Nov 02 '12 at 07:47
  • 2
    If your code is all in the top-level class, then you wrote it to be untestable. Bad idea. – John Saunders Nov 02 '12 at 07:48
  • Why is it untestable if it's in the top-level class? Also, how can something be 'untestable'? Isn’t it simply a degree of how easy it is multiplied by the capabilities of your testing tools? – Allon Guralnek Nov 02 '12 at 07:51
  • Clearly, you're not familiar with unit testing, or testability in general. – John Saunders Nov 02 '12 at 07:54
2

If you need to test logic behind services you can write simple unit tests like shown below:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}
//Tests
[TestFixture]
public class MyService_Test
{
    [Test]
    public void GetData_should_return_entered_string()
    {
        Service1 service = new Service1();
        Assert.AreEqual("You entered: 1", service.GetData(1));
    }
}

And if you want to test whole integration you can write the following integration tests. In a nutshell you need to run your service as self-hosted and use _proxy to execute service methods. Such tests are useful when you need to test extensibility points like custom message inspector, error handlers, etc.

    private ITestService _proxy;
    private ServiceHost _host;

    [SetUp]
    public void Initialize()
    {
        const string baseAddress = "net.pipe://localhost/TestService";
        _host = new ServiceHost(typeof(TestService), new Uri(baseAddress));

        var factory = new ChannelFactory<ITestService>(new NetNamedPipeBinding(),
                                                       new EndpointAddress(baseAddress));
        _host.Open();
        _proxy = factory.CreateChannel();
    }

Links:

Integration Testing WCF Services

k0stya
  • 4,267
  • 32
  • 41
1

If you just want to test the service's logic, simply instantiate the class that implements the logic and call its methods. This essentialy bypasses WCF, which is good for unit tests since there is no reason to test Microsoft's code. Integration tests, on the other hand, are a different matter.

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93