Tag Archives: Testing

Random values generator for different needs

Currently I’m working on a large Silverlight project where we often need random values for testing purpose during UI development or unit testing.

Recently I’ve implemented a RandomProvider class to generate random values for different needs such as simple values like int, short, long, byte, DateTime, Timespan, Thickness, string as well as complex types such as words, addresses, phone numbers, company names, email adresses, names and so on.

 

image

 

Below is a list with the common method implemented in the RandomProvider class.

    RandomProvider.NextBoolean();
    RandomProvider.NextShort();
    RandomProvider.NextInt();
    RandomProvider.NextLong();
    RandomProvider.NextUShort();
    RandomProvider.NextUInt();
    RandomProvider.NextULong();
    RandomProvider.NextDouble();
    RandomProvider.NextSingle();
    RandomProvider.NextDateTime();
    RandomProvider.NextDate();
    RandomProvider.NextTimeSpan();
    RandomProvider.NextThickness();
    RandomProvider.NextString();

    // returns a random string from the given input parameters
    RandomProvider.NextString("value1", "value2", "value3");

    // generates a "lorum ipsum" word combination
    RandomProvider.NextWords(10);

    // returns a value from the given input parameters
    RandomProvider.NextValue<FontWeight>(FontWeights.Normal, FontWeights.Black, FontWeights.Bold);

    // returns a random enum value from the given enum type 
// If the enum is using the FlagsAttribute (enum values as bit flags),
// it may return a combination of these enum values.

RandomProvider.NextEnum(typeof(RandomProviderTestEnum)); RandomProvider.NextColor(); RandomProvider.NextSystemColor(); RandomProvider.NextSolidBrush(); RandomProvider.NextLinearGradientBrush(); RandomProvider.NextFirstname(); RandomProvider.NextLastname(); RandomProvider.NextSalutation(); RandomProvider.NextEMail(); RandomProvider.NextWebLink(); RandomProvider.NextPhoneNumber(); RandomProvider.NextStreet(); RandomProvider.NextCity(); RandomProvider.NextCompany(); // returns a complex object with firstname, lastname, salutation, eMail, weblink, street, city name, company. // email, phone number and weblink are connected to firstname, lastname and the company. RandomProvider.NextContact();

 

Most of the methods above provides on overloaded signature which takes the minimum and maximum range of the random value. See the example below:

    RandomProvider.NextInt(10, 10000); 
    RandomProvider.NextTimeSpan(TimeSpan.FromMilliseconds(1), TimeSpan.FromMinutes(1)); 
    RandomProvider.NextWords(10, 1000); 

 

Have a lock to this small demo application to get a feeling what the RandomProvider class gives.

You can download the source here

Have fun.

Advertisements
Tagged , ,