简体   繁体   中英

Unit Testing WCF Service is launching

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();
        }
    }

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM