Wcf – Unit testing database-dependent Window services

databasenunitunit testingwcf

We have a set of services in .NET 3.5\C# and WCF.
The NUnit tests need the services to be running and listening for requests.
The services need an updated SQL database to be ready for connection.

Currently the [SetUp] section of the unit test does two tasks:

  • Execute the latest SQL scripts to build the database.
  • Utilize a
    System.Diagnostics.Process.Start to run the commandline mode of the services.

It usually works but the services are sensitive for certain schema changes, which sometimes fails them. I'm looking for the best practice for setup the database and then the services, and also making sure the services are down at the end.

The process is run by MSBuild.

Best Answer

If you're starting the services, and hitting actual executing services...changes are you're not just Unit testing anymore. You're now integration testing.

You should really think about abstracting your Data Access into an Interface. You can then code a concrete implementation of that Interface for normal operation and use Dependency Injection to inject a mock implementation for your Unit tests.