Property Information

I would like to mock the object data, not the objects themselves. In other words, I would like to generate a collection of n objects and pass it to a function that generates random rows of data and numbers. Is there anything for this? Think of it like Lorem Ipsum for object data. Limitations around number ranges, etc. Not needed, but will be a bonus.

+4
source share
5 answers

The first thing I thought when I read your question is QuickCheck, a testing tool for Haskell. In QuickCheck, you specify the properties (invariants) that your function should have, and you can specify valid input ranges (plus many additional functions), and QuickCheck will create a bunch of random input data and throw it at your function and check if the output matches the specification . Sinking down a bit, I discovered that there is an F # port, so QuickCheck exists in the .net world:

http://fscheck.codeplex.com/

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

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

"... Pex finds interesting I / O values ​​for your methods that you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-on for testing .NET Framework applications."

I have not used it before, but it looked like it was good for generating data about extreme cases that use any function branches. He actually analyzes the function, and not just throws really random material at it.

+5
source

There is also a .NET port for Ruby Faker , which is very useful for falsifying data objects in Ruby. I have not used it, but it may be worth considering:

https://github.com/slashdotdash/faker-cs

+4
source

I am surprised that no one has mentioned AutoFixture yet :

 [TestMethod] public void IntroductoryTest() { // Fixture setup Fixture fixture = new Fixture(); int expectedNumber = fixture.CreateAnonymous<int>(); MyClass sut = fixture.CreateAnonymous<MyClass>(); // Exercise system int result = sut.Echo(expectedNumber); // Verify outcome Assert.AreEqual<int>(expectedNumber, result, "Echo"); // Teardown } 
+3
source

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

Alternatively, you can create a set of classes for each data object that generates random data for them. This may work if you do not want to include random generation methods in actual assemblies. For example, if you have a Person class, you can have a PersonRandomizer class in a test assembly. In your test class (or in the Randomizer class), you can use reflection to find the PersonRandomizer type, and if it exists, call PersonRandomizer.Randomize(Person p) .

If you come with the yahya clause, I suggest creating an IRandomizable interface for objects that support randomization or label them with a new Randomizable attribute that you can detect at runtime.

+2
source

For my testing, I just add a method to all the classes that I create and do it randomly. Each class knows what the legal data should look like for an object of this class. After creating an object, simply call its randomization method to fill it with random layout data. You can also add specialized methods to generate random rows of data or numbers based on constraints, and these specialized methods may be available for all your classes.

+1
source

Source: https://habr.com/ru/post/1309589/


All Articles