Tuesday, 17 April 2012

ViewModel level acceptance testing

In the past, when writing acceptance tests for a project, I've usually used a GUI automation tool such as White (for desktop) or Selenium RC (for web apps). For this project I wanted to try something a bit different. I wanted take full advantage of the fact that I'm using the M-V-VM pattern and start acceptance test cases from the ViewModel rather than the View.

So instead of the tests firing up an instance of the compiled app, I've created an acceptance test bootstrapper that creates all the unity mappings that the application bootstrapper does, but replacing the mappings for UI related resources with RhinoMocks mock objects. This bootstrapper is called by the SpecFlow test framework before the tests start. This means that I can run virtually end-to-end tests on the application without any screen/keyboard/mouse interaction. This sort of test suite is ideally suited to being run on a build machine.

Another difference to the automation approach was that I change the way the database was initialised when the tests start. Usually the database will be dropped and rebuilt each time the model changes. For the acceptance testing, the database is re-created on each run. This provides a consistent data environment for the test to run in. To get this to work, I needed to set up a separate NUnit project that reloaded the test assembly for each run. This ensured that the Unity container would be re-created and the DBContext object singleton would initialise the database.

No comments:

Post a Comment