2

I'm new to WCF. I've created a basic service and engineer tested it with the debugger and WCFTestClient. I've never written my own WCF client. Now I need to build unit tests for the service.

My classes:

IXService
CXService
CServiceLauncher

(Yes, I know the C prefix does not meet current standards, but it is required by my client's standards.)

My service functionality can be tested directly against XService, but I need to test CServiceLauncher as well. All I want to do is connect to a URI and discover if there is a service running there and what methods it offers.

Other questions I read:

Test outline:

    public void StartUiTest()
    {
        Uri baseAddress = new Uri("http://localhost:8080/MyService");
        string soapAddress = "soap";
        IUserInterface target = new CServiceLauncher(baseAddress, soapAddress);
        try
        {
            Assert.AreEqual(true, target.StartUi());
            /// @todo Assert.IsTrue(serviceIsRunning);
            /// @todo Assert.IsTrue(service.ExposedMethods.Count() > 4);
            Assert.Inconclusive("This tells us nothing about the service");
        }
        finally
        {
            target.StopUi();
        }
    }
Community
  • 1
  • 1
Denise Skidmore
  • 2,286
  • 22
  • 51
  • 4
    This is integration test and not unit test – oleksii May 09 '13 at 19:31
  • Well, without re-factoring the code to allow injection of the service implementation object into the ServiceLauncher, how would you suggest converting it to a unit test? – Denise Skidmore May 09 '13 at 19:45
  • To test `CServiceLauncher` I would mock a service and verify that say a `Start` method was called. If it takes more than 10-15 lines per test, I would stop and rethink how can mock the thing to allow me to write a test in 10-15 lines. – oleksii May 09 '13 at 20:10
  • Even if we inject a mock service into CServiceLauncher, we still are in the position of needing to see if the injected service was launched. – Denise Skidmore May 09 '13 at 20:19

2 Answers2

1

I just needed to build a simple client.

Reference: http://webbeyond.blogspot.com/2012/11/wcf-simple-wcf-client-example.html

  1. Add Service Reference to test project
  2. Add to test file:

    using System.ServiceModel;

    using MyTests.ServiceReferenceNamespace;

Code inside try is now:

            Assert.AreEqual(true, target.StartUi());
            XServiceClient client = new XServiceClient();
            client.GetSessionID();
            Assert.AreEqual(CommunicationState.Opened, client.State, "Wrong communication state after first call");
Denise Skidmore
  • 2,286
  • 22
  • 51
  • FYI, the first State test does not test that the service was started. If the service was not started I get an exception when I call client.GetSessionID(). `client.Endpoint.Contract.Operations.Count` fulfills my earlier suggestion of getting `service.ExposedMethods.Count()` but also works fine even if the service is not running. I'll still accept other answers if someone can suggest a better way than exceptions to detect if the service is running. – Denise Skidmore May 09 '13 at 21:12
  • client.Open(); opens the connection whether the service is running or not. No exceptions, no failure when asserting client.State. – Denise Skidmore May 09 '13 at 21:19
0

It's not a real answer so please take it easy.

I have been trying to do similar things and what I have learnt that integration testing is difficult. It is difficult because there are many hidden tasks that you need to do, such as:

  • Make sure you can run the tests regularly
  • Make sure integration tests can run on the test environment
  • Maintain different config files, as your environment will be different from the test one
  • Configure the thing that would automate running of integration tests (CI)
  • Pray there will be no changes to the paths, test environment, config, hosting platforms etc
  • Fight security permissions, as usually test thing is not able to host WCF services without admin permissions
  • Maintain your test harness

To me, this was huge headache and little gain. Don't get me wrong, integration testing is a positive thing, it just requires a lot of time to develop and support.

What have I learnt? Is that do not bother with integration testing of WCF services. Instead I write a lot of unit-tests, to test the contract, state and behaviour. By covering those, I can become sure in a quality of software. And I fight integration of WCF during deployment. This is usually a single battle to configure environment or VM and then next time, deployment goes nice and smooth in an (semi-)automated manner.

Most people would also automate deployment with Chef and alike, those tools can fully configure environment and deploy WCF service.

oleksii
  • 35,458
  • 16
  • 93
  • 163