God, I love this stuff.
So I sit down, and in less than an hour build a simple model-based testing program for my Rock Paper Scissors web service.
Basically, I start by defining all the valid states for my service.
private enum states
{
Offline,
ShakeHands,
TakeTurn,
ReceiveTurnFeedback,
ReceiveGameFeedback
};
Then I create an array indicating all the valid state from-to combinations.
private states [] testorders =
{states.Offline, states.ShakeHands,
states.ShakeHands, states.TakeTurn,
states.ShakeHands, states.ReceiveGameFeedback,
states.TakeTurn, states.ReceiveTurnFeedback,
states.ReceiveTurnFeedback, states.TakeTurn,
states.ReceiveTurnFeedback, states.ReceiveGameFeedback,
states.ReceiveGameFeedback, states.Offline};
So, taking the first line of the array above, if the rps service is currently offline, the only valid action would be to shake hands. If you have just shaken hands, you have two choices: you can either take a turn, or receive game feedback (close the game down).
The actual code is rather simple. If my RPS service were a professional application, I would improve this a bit. Basically, I keep track of the current state, loop through the array looking for all the possible actions, and then randomly take one.
// What can my next states be?
ptr = 0;
for (int j = 0; j < testorders.Length - 1; j += 2)
{
if (testorders[j] == CurrentState)
{
// Add this to the list
NextState[ptr++] = testorders[j+1];
}
}
// Pick one
CurrentState = NextState[rndm.Next(0, ptr - 1)];
If I do this enough times, I will have tested every allowable combination of actions and states. And somehow there was a new allowable action, for instance, SayNiceGame(), I could add that to my states array easily enough.
Possible improvements to my application would include testing for exceptions, and handling them gracefully; keeping statistics about the number of tests run; calculating the percentages of each choice made (rock, paper and scissors)... You can start with a basic test model, and make it more complex as you have time.
Testing Web Service, v1.0 - C# Source Code
Testing Web Service, v1.0 - .NET Binary