views:

149

answers:

3

I'd like to mock up object data, not the objects themselves. In other words, I would like to generate a collection of n objects and pass it into a function which generates random data strings and numbers. Is there anything to do this? Think of it as a Lorem Ipsum for object data. Constraints around numerical ranges etc. are not necessary, but would be a bonus.

+1  A: 

For my testing I just add a method to all the classes I create that does this and call it randomize. Each class knows what the legal data for an object of that class should look like. After creating the object simply call its randomize method to populate it with random mock up data. You could also add specialized methods to generate random data strings or numbers based on constraints and these specialized methods can be shared across all your classes.

yahya
I thought about doing this on a base class and using reflection to generate adaquate data, but I'd like something that already exists and can be plugged in. Maybe this is something I should create.
Nissan Fan
+1  A: 

You could setup an ObjectRandomizer class that takes an array of objects, uses reflection to examine the object for private members, and then use reflection to set a random value for that member. This works only if you don't care what the random data looks like per object.

Alternatively, you could build a set of classes for each data object that generates random data for them. This might work well if you didn't want to include the random generation methods inside the actual assemblies. For example, if you had a Person class, you could have a PersonRandomizer class in a test assembly. In your test class (or in a Randomizer class), You could then use reflection to find a type PersonRandomizer, and if it exists, call PersonRandomizer.Randomize(Person p).

If you go with yahya's suggestion, I suggest creating an interface IRandomizable for objects that support randomization OR marking them with a new attribute Randomizable that you can detect at runtime.

Paul Williams
+2  A: 

The first thing I thought of when I read your question was QuickCheck, a testing tool for Haskell. In QuickCheck you specify properties (invariants) that your function should have, and you can specify valid ranges for inputs (plus a bunch more features), and QuickCheck will generate a bunch of random input data and throw it at your function and check too see if the output matches the specification. Nosing around a little I found out there is an F# port of it so QuickCheck exists in the .net world:

http://fscheck.codeplex.com/

There is also a MS Research project Pex that might be close to what you are thinking of:

http://research.microsoft.com/en-us/projects/Pex/

"...Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications."

I haven't used it before, but it looked like it is good for generating edge case data that exercises any and all branches of a function. It actually analyzes a function rather than just throwing truly random stuff at it.

Travis Brooks