Night Walker
Night Walker

Reputation: 21280

hosting webservice in unitTest

I want to host WCF service inside unit Test using ServiceHost.

And then running them in on a build machine.

Is it possible to host web services like this ? if yes how it's done ?

Thanks

Upvotes: 0

Views: 2243

Answers (3)

CodingWithSpike
CodingWithSpike

Reputation: 43728

Sure, you just use a ServiceHost like you would if it was a self-hosted wcf service. I typically do this in a test fixture/class setup/teardown.

For example with NUnit:

[TestFixture]
public class MyTests
{
    private ServiceHost service;

    [TestFixtureSetUp]
    public void FixtureSetUp()
    {
        service = new ServiceHost(typeof(MyService));
        service.Open();
    }

    [Test]
    public void ThisIsATest()
    {
        using (var client = new MyServiceClient())
            client.DoStuff(); // test whatever
    }

    [TestFixtureTearDown]
    public void FixtureTearDown()
    {
        if (service != null)
        {
            if (service.State == CommunicationState.Opened)
                service.Close();
            else if (service.State == CommunicationState.Faulted)
                service.Abort();
        }
    }
}

You will also need to copy any WCF XML configuration into the test assembly's app.config.

One thing to note however is the threading model of your unit test framework. If it is possible for your test runner to run multiple test classes simultaneously, then you might try to open the same service on the same port more than once, and it might fail.

On a final note, I would typically separate tests that do and don't fire up the actual WCF service. Put tests that don't hit WCF into a separate "Unit Tests" assembly, that runs without dependencies and runs fast, and put ones that do use WCF into an "Integration Tests" assembly instead. That is just a recommendation of course, not a rule.

Upvotes: 3

Vitaliy
Vitaliy

Reputation: 8206

It is indeed possible and actually is the same as hosting a WCf service in a console application! Add your app.config to the UT assembly and proceed as always. (You can also configure programatically of course). Similarly, you create a client for the host in code.

Instantiate and open your host and client on the set-up method, you can do this per test or per class, depending on the level of isolation you seek.

So you see, you don't have to have a separate deployment stage.

Having said all the above, I would actually discourage you from working with services in your Unit tests. One of the best practices concerning unit tests is that they should be very fast to run. Making calls to web services (even local) is very costly in UT.

Moreover, this kind of contradicts the semantics of a UNIT test. You should be testing small bits of functionality. And strive to mock out external dependencies such as Database access, External services and even other classes. I would be happy to elaborate and refer to more information if you feel like it.

Upvotes: 2

paulik
paulik

Reputation: 119

I think the problem here is that UnitTests are usually just a set of binaries that are executed by some other framework (NUnit, xUnit,...).

You can try on the TestSuite setup start another Thread and run WCF on that Thread, but I believe it is much more involved into this approach and Test suites may no like it as well.

What I would do is to setup a deployment step on your build machine of the WCF service to IIS or redeployment to the Windows Service hosted WCF before running UnitTests.

Upvotes: 0

Related Questions