Create Test Data in Code
I am writing an exporting function to take data from a database table and export to text. Before I plug the code into the rest of the app I would like to test the export based on random data created in c#. I have found many examples in SO about creating test dat开发者_如何学JAVAa in a database but none that was created directly in code. Does anybody have an example or know of a link to one?
Thanks
You can use AutoFixture to generate pseudo-random data directly from C# code.
It is a convention-based and extensible library that uses reflection to populate objects with data.
It can be used as simply as this:
Fixture fixture = new Fixture();
MyClass mc = fixture.CreateAnonymous<MyClass>();
After the second line of code, the mc
instance will be populated with all appropiate constructor parameters, and all writable properties will have been assigned.
It supports nested hierachies, non-default constructors, mapping from interfaces to concrete types and many other things.
You can customize it (almost) to your heart's content as well.
You can try NBuilder if you haven't already.
精彩评论