WCF Unit Test

前端 未结 8 665
误落风尘
误落风尘 2020-12-12 19:38

How to unit test WCF services? Any 3rd Party tools available?

相关标签:
8条回答
  • 2020-12-12 19:49

    What exactly do you want to test? Connectivity or service methods?

    Cool thing about WCF is that you can just define interfaces (err, contracts) and test them as regular code. Then you can assume that they will work via any connection type supported by WCF.

    Connectivity can be tested by hosting your service directly in UT or on development web-server.

    As for tools, you there are tons of unit testing frameworks: NUnit, built-in tests in Visual Studio, xUnit, etc, etc.

    You can download "Visual Studio 2008 and .NET Framework 3.5 Training Kit" and ".NET Framework 3.5 Enhancements Training Kit" if I recall correctly there were samples for WCF unit tests

    0 讨论(0)
  • 2020-12-12 19:51

    You can use Typemock Isolator to do that. Here are a couple of posts on the issue of testing the client side, and the server side. You can do that without any dependency, including a config file.

    Gil Zilberfeld Typemock

    0 讨论(0)
  • 2020-12-12 19:58

    Typemock Isolator is a tool for unit testing wcf services, amoung other things...

    0 讨论(0)
  • 2020-12-12 19:59

    As aku says, if you're testing service methods (ie code behaviour) then you can unit test that directly and bypass the WCF infrastructure. Of course, if your code depends on WCF context classes (like OperationContext) then I suggest introducing wrappers much like ASP.NET MVC does for HttpContext.

    For testing connectivity, it will depend on the type of endpoints you have configured. In some cases you can just self-host your WCF service inside the unit test (like you would with a WCF Windows Service) and test that.

    However, you may need to spin up the ASP.NET Development Web Server or even IIS if you want to test WCF behaviour specific to those hosting environments (ie SSL, authentication methods). This gets tricky and can start making demands on the configuration of everyone's development machine and the build servers but is doable.

    0 讨论(0)
  • 2020-12-12 19:59

    I think the best approach is to separately test all concerns; test connectivity, client lib (proxies) and service method calls. Mocking and dependency injection is a good way to tests connectivity and service behaviour independently, but I doubt that it can get around middleware dependent endpoint tests.

    You can create a service host in your test (self-hosted) and load you service. Once you set up your end points you can connect to it using your client proxies. This should work with simple HTTP and WSHTTP. In your Unit test you need to create a service reference to your service. Then you can create a host and wire your client with the test host together. I would try to avoid any tests using the "WCF Service Host" aka WcfSvcHost. (I mention this only because some people referred to the Visual Studio utils; which will work only if you only run tests from your IDE.)

    If you need to check exotic authentication scenarios or endpoints which use special middleware you will need to create tests using the middleware. For simple sanity checks, etc using self-hosting is sufficient. Middleware dependent testing can cause test deployment issues if you are using a build server.

    By middleware dependent endpoints I mean endpoints which are using for example MOMs (MSMQ, RabbitMQ, etc) or really exotic protocols, etc. Perhaps testing the client proxies with a self-hosted mock and testing the exotic endpoints separately is the way to go.

    If you want to use dependency injection there are a few pretty sophisticated frameworks which provide “service abstraction” features which allow you to inject mock services, etc. I used Spring.NET with WCF a few times. Castle Windsor has WCF facilities as well.

    Self-hosted test example:

        ServiceHost serviceHost = null;
    
        try
        {
            var baseAddress = new Uri("http://localhost:8000/TestService");
            serviceHost = new ServiceHost(typeof (ServiceClass), baseAddress);
            Binding binding = new WSHttpBinding();
            var address = new EndpointAddress("http://localhost:8000/TestService/MyService");
            var endpoint = serviceHost
                .AddServiceEndpoint(typeof (IServiceContract), binding, address.Uri);
    
            var smb = new ServiceMetadataBehavior {HttpGetEnabled = true};
            serviceHost.Description.Behaviors.Add(smb);
    
            using (var client = new ProxyClient(endpoint.Name, endpoint.Address))
            {
                endpoint.Name = client.Endpoint.Name;
    
                serviceHost.Open();
    
                // ... magic happens 
            }
    
            serviceHost.Close();
        }
        catch (Exception ex)
        {
            // ... tests
        }
        finally
        {
            if (serviceHost != null)
            {
                ((IDisposable) serviceHost).Dispose();
            }
        }
    

    I would like to point out that functional testing tools are not the same as unit testing tools. Unit testing should be about breaking down your test into a bunch of independent tests while functional testing is mostly about testing workflows end to end.

    0 讨论(0)
  • 2020-12-12 20:02

    If one really wants to test WCF services, it's best to go with integration tests that actually exercise the client-server connectivity part of it.

    0 讨论(0)
提交回复
热议问题