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