Paulo Morgado

Using Random Values For Unit Testing

When writing my unit tests I don’t like to use hard coded fixed values because I either end up using the same values or, because of that, tests may succeed by coincidence.

Over time, I have developed an helper class to generate random values for testing.

namespace PauloMorgado.VisualStudio.TestTools.UnitTesting
{
    public static class RandomGenerator
    {
        public static bool Boolean();
        public static string String();
        public static string String(string prefix);
        public static short Int8();
        public static short Int8(short maxValue);
        public static short Int8(short minValue, short maxValue);
        public static short Int16();
        public static short Int16(short maxValue);
        public static short Int16(short minValue, short maxValue);
        public static int Int32();
        public static int Int32(int maxValue);
        public static int Int32(int minValue, int maxValue);
        public static TEnum Enum<TEnum>();
        public static TEnum EnumFlagsWith<TEnum>(TEnum flagsToAdd);
        public static TEnum EnumFlagsWithout<TEnum>(TEnum flagsToRemove);
        public static TEnum Enum<TEnum>(int maxValue);
        public static TEnum Enum<TEnum>(int minValue, int maxValue);
        public static System.Guid Guid();
    }
}

This is something that I would like to find on mock frameworks (like Typemock Isolator, Rhino.Mocks or MoQ).

It’s still a work in progress, but if you want to try it, it’s on my MSDN Code Gallery: Random Generator For Unit Testing

Comments

Anders Cui said:

It's cool, i will try it!

# June 24, 2008 5:39 AM

Paulo Morgado said:

Thanks Anders. Let me know what you think of it after using it.

# June 24, 2008 6:27 AM

Petar Petrov said:

Great helper !

However I will suggest some methods for the string generation

public static string String(int length);

and probably

public static string String(char[] allowedSymbols);

public static string String(char[] allowedSymbols, int length);

Just one more thing, please extend the helper to support Int64.

# June 24, 2008 6:58 AM

Paulo Morgado said:

Nice suggestions, Petar.

I'll add them as soon as I can.

# June 24, 2008 6:04 PM

Jason Stangroome said:

Hi,

I agree with randomised unit tests to get theoretically better coverage of values but I always ensure the tests publish the random seed when they fail so I can rerun the test with the same seed and get it to fail the same way in debug.

Regards,

--

Jason

# June 25, 2008 12:17 AM

Paulo Morgado said:

Don't you just need the actual used values? Aren't those that make the test pass or fail?

You can always add a System.Diagnostics.Trace.WriteLine call to record the values. Even for non random ones like when using a data set.

# June 25, 2008 5:16 AM

Phi Le said:

@Paulo: Wouldn't you then you have to manually sprinkle TraceLine()'s throughout your test code?

Printing the random seed just once seems much easier way to investigate a failed test.

# June 26, 2008 8:33 AM

Paulo Morgado said:

On a previous post I introduced a library for generating random values for purposes of unit testing.

# June 29, 2008 7:40 PM

Paulo Morgado said:

On a previous post I introduced a library for generating random values for purposes of unit testing.

# June 29, 2008 7:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)