Search code examples
c#.netunit-testingwcf

Unit Test WCF Service with long starting time


We have a WCF-service with a starting time of several minutes, because a lot of data must be loaded in memory.

Our questions is, how do we test all of our methods without several restarts of the service?

In detail:

  • We test the methods of the service contract from another WCF-service, so that we only have to start our WCF-service once. I.e.,, we start our service and the test-service and the latter calls our service for running the related unit tests.

  • For the methods which do not require the data in memory we simply implemented unit tests inside the service.

  • Our problem relates to all methods which require the data in memory and are not part of the data contract. How do we have to implement unit tests for these methods, so that we can run several tests and the service must be started only once? Actually, we implemented these unit tests inside the service, but if we run a test the service has to start and shuts down as soon as the test is finished.

Please note, that it is not possible to move the related methods into a library or to add them to the service contract.


Solution

  • If I understand you correctly, the main problem you're experiencing is dealing with the state of your WCF service, since it requires it on startup.

    First of all, it's good to see that you implemented separate unit tests for all test cases that don't rely on the state of your service.

    When it comes to testing the stateful methods, you'll need integration tests (but I do not want to do a hair-splitting because of the terminology 😊). To do so, you would initialize your service in a setup-method only once, prior to executing any tests (that subsequently may be dependant on each other).

    Which test framework are you using? NUnit, MSTest or any other? Assuming you're using NUnit, you'd use the [OneTimeSetUp]-attribute to initialize your WCF service and use that instance for each test that requires the service (and the state, respectively the data) to be initialized:

    namespace Foo
    {
      [TestFixture]
      public class YourServiceTests
      {
        private IYourService _service;
    
        [OneTimeSetUp]
        public void Init()
        { 
          /* Do whatever is necessary to initialize the service */ 
          _service = CreateNewInstance();
        }
    
        [OneTimeTearDown]
        public void Cleanup()
        { 
          /* optionally provide a teardown method to gracefully shutdown the service */ 
        }
    
        [Test]
        public void TestA()
        { 
          var expected = "bar";
          var actual = _service.MethodA();
          Assert.That(actual, Is.EqualTo(expected));
        }
    
        [Test]
        public void TestB()
        { 
          // ...
        }
      }
    }
    

    --

    Regarding your other concern ("Our problem relates to all methods which require the data in memory and are not part of the data contract."), I'd use a similar approach:

    Since you want to test the methods which are not part of the IYourService-contract you'd basically do the same as above ([OneTimeSetUp]), but now you'd initialize the concrete implementation of IYourService (e.g. YourService). You'd then be able to access and test all other public methods which wouldn't be accessible via the interface/contract.