模拟对象数据
我想模拟对象数据,而不是对象本身。换句话说,我想生成 n 个对象的集合并将其传递给一个生成随机数据字符串和数字的函数。有什么可以做的吗?将其视为对象数据的 Lorem Ipsum。数字范围等的限制不是必需的,但会是一个好处。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我读到你的问题时,我首先想到的是 QuickCheck,一个 Haskell 的测试工具。在 QuickCheck 中,您指定函数应具有的属性(不变量),并且可以指定输入的有效范围(加上更多功能),QuickCheck 将生成一堆随机输入数据并将其扔到您的函数中并检查也请参阅如果输出符合规格。仔细观察后,我发现它有一个 F# 端口,因此 QuickCheck 存在于 .net 世界中:
http://fscheck .codeplex.com/
还有一个 MS Research 项目 Pex 可能与您的想法很接近:
http://research.microsoft.com/en-us/projects/Pex/
"...Pex 找到您的方法中有趣的输入输出值,您可以将其另存为具有高代码覆盖率的小型测试套件,Microsoft Pex 是一个用于测试 .NET Framework 应用程序的 Visual Studio 插件。”
我以前没有使用过它,但看起来它非常适合生成执行函数的任何和所有分支的边缘情况数据。它实际上分析一个函数,而不是仅仅向它扔真正随机的东西。
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.
Ruby
Faker
gem 似乎还有一个 .NET 端口,它被广泛用于在 Ruby 中操纵虚假数据对象。我没有使用过它,但可能值得研究一下:https://github.com/slashdotdash/ faker-cs
There also appears to be a .NET port of the Ruby
Faker
gem, which gets a lot of use for rigging fake data objects in Ruby. I haven't used it, but it might be worth looking into:https://github.com/slashdotdash/faker-cs
我很惊讶没有人提到 AutoFixture :
I am surprised that no one has mentioned AutoFixture yet:
您可以设置一个
ObjectRandomizer
类,该类采用对象数组,使用反射检查对象中的私有成员,然后使用反射为该成员设置随机值。仅当您不关心每个对象的随机数据是什么样子时,这才有效。或者,您可以为每个数据对象构建一组类,为它们生成随机数据。如果您不想在实际程序集中包含随机生成方法,这可能会很有效。例如,如果您有一个
Person
类,则测试程序集中也可以有一个PersonRandomizer
类。在您的测试类中(或在Randomizer
类中),您可以使用反射来查找类型PersonRandomizer
,如果存在,则调用PersonRandomizer.Randomize(人p)
。如果您同意 yahya 的建议,我建议为支持随机化的对象创建一个接口
IRandomizable
,或者使用您可以在运行时检测到的新属性Randomizable
标记它们。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 aPersonRandomizer
class in a test assembly. In your test class (or in aRandomizer
class), You could then use reflection to find a typePersonRandomizer
, and if it exists, callPersonRandomizer.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 attributeRandomizable
that you can detect at runtime.对于我的测试,我只是向我创建的所有类添加一个方法来执行此操作,并将其称为随机化。每个类都知道该类的对象的合法数据应该是什么样子。创建对象后,只需调用其 randomize 方法即可用随机模拟数据填充它。您还可以添加专门的方法来根据约束生成随机数据字符串或数字,并且这些专门的方法可以在所有类之间共享。
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.