Web Design. Development. Optimization. RSS 2.0
 Wednesday, June 02, 2004

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

 

Wednesday, June 02, 2004 12:54:08 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
.NET | Demo Code
Del.icio.us Digg Technorati Blinklist Furl reddit
Comments are closed.
Archive
<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Scott Duffy
Sign In
Statistics
Total Posts: 470
This Year: 30
This Month: 0
This Week: 0
Comments: 73
Themes
Pick a theme:
All Content © 2008, Scott Duffy
DasBlog theme 'Business' created by Christoph De Baene (delarou)