用随机数据填充对象的C#库
我想用随机数据填充我的对象(出于测试目的),是否有库可以这样做 某些类型的反射方法将遍历对象图并初始化基本属性,如(string、int、DateTime等)(但要深入操作,包括集合、子对象等)有一些功能,它不使用反射,而是告诉它要填充的数据类型。因此,如果您正在编写单元测试,可以在用随机数据填充对象的C#库,c#,C#,我想用随机数据填充我的对象(出于测试目的),是否有库可以这样做 某些类型的反射方法将遍历对象图并初始化基本属性,如(string、int、DateTime等)(但要深入操作,包括集合、子对象等)有一些功能,它不使用反射,而是告诉它要填充的数据类型。因此,如果您正在编写单元测试,可以在[Setup]或[TestInitialize]方法中完成。是生成数据的非常好的fluent API库。它使用您定义的规则,本身不是“随机”的。不过,您可以随机化API的输入,以满足您的需要 由于这仍然受到一些关注,
[Setup]
或[TestInitialize]
方法中完成。是生成数据的非常好的fluent API库。它使用您定义的规则,本身不是“随机”的。不过,您可以随机化API的输入,以满足您的需要
由于这仍然受到一些关注,我认为值得一提的是,该项目现在也可以通过NuGet()获得,尽管它自2011年以来一直没有被修改过。NBuilder非常不错 我相信它也使用反射。制作了一个名为。如果您愿意使用数据库作为测试对象的种子,我想您会发现它是一个非常灵活的工具。我尝试了AutoFixture(),它对我非常有效。 它可以很容易地在一行代码中生成具有深层子层次结构的对象
PM> Install-Package NBuilder
注意:EducationInformation类本身有很多字符串属性
var rootObject = new RootObject()
{
EducationInformation = Builder<EducationInformation>.CreateNew().Build(),
PersonalInformation = Builder<PersonalInformation>.CreateNew().Build(),
PositionsInformation = Builder<PositionsInformation>.CreateNew().Build()
};
注意:我不知道为什么使用以下命令会为所有内部类返回null
RootObject rootObject = Builder<RootObject>.CreateNew().Build()
RootObject RootObject=Builder.CreateNew().Build()
由于有些库已经过时或不再开发,我创建了自己的库,可以使用随机或自定义数据填充类。我最近一直在开发一个与您描述的完全相同的库(可能以前已经做过,但这似乎是一个有趣的尝试项目). 这项工作仍在进行中,但我认为它涵盖了您提到的所有功能。您可以在此处找到Nuget软件包:
这里是存储库:
我希望你喜欢
示例代码:
var randomMyClass=RandomValue.Object()代码>
是一个用于C#和.NET的简单而理智的伪数据生成器。一个C#端口,受FluentValidation语法sugar的启发。支持.NET核心
设置
public enum Gender
{
Male,
Female
}
var userIds = 0;
var testUsers = new Faker<User>()
//Optional: Call for objects that have complex initialization
.CustomInstantiator(f => new User(userIds++, f.Random.Replace("###-##-####")))
//Basic rules using built-in generators
.RuleFor(u => u.FirstName, f => f.Name.FirstName())
.RuleFor(u => u.LastName, f => f.Name.LastName())
.RuleFor(u => u.Avatar, f => f.Internet.Avatar())
.RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName))
.RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName))
//Use an enum outside scope.
.RuleFor(u => u.Gender, f => f.PickRandom<Gender>())
//Use a method outside scope.
.RuleFor(u => u.CartId, f => Guid.NewGuid());
没有流畅的语法
public void Without_Fluent_Syntax()
{
var random = new Bogus.Randomizer();
var lorem = new Bogus.DataSets.Lorem();
var o = new Order()
{
OrderId = random.Number(1, 100),
Item = lorem.Sentence(),
Quantity = random.Number(1, 10)
};
o.Dump();
}
/* OUTPUT:
{
"OrderId": 61,
"Item": "vel est ipsa",
"Quantity": 7
} */
如果您使用的是.NET,则可以使用(),这是一个免费的、开源的.NET动态框架
它与.NET Core兼容。是一个简单的基于反射的工具,用于生成和填充具有随机值的对象
[TestMethod]
public void GenerateTest()
{
RPGenerator gen = new RPGenerator();
int maxRecursionLevel = 4;
var intRes = gen.Generate<int>(maxRecursionLevel);
var stringArrayRes = gen.Generate<string[]>(maxRecursionLevel);
var charArrayRes = gen.Generate<char[]>(maxRecursionLevel);
var pocoRes = gen.Generate<SamplePocoClass>(maxRecursionLevel);
var structRes = gen.Generate<SampleStruct>(maxRecursionLevel);
var pocoArray = gen.Generate<SamplePocoClass[]>(maxRecursionLevel);
var listRes = gen.Generate<List<SamplePocoClass>>(maxRecursionLevel);
var dictRes = gen.Generate<Dictionary<string, List<List<SamplePocoClass>>>>(maxRecursionLevel);
var parameterlessList = gen.Generate<List<Tuple<string, int>>>(maxRecursionLevel);
// Non-generic Generate
var stringArrayRes = gen.Generate(typeof(string[]), maxRecursionLevel);
var pocoRes = gen.Generate(typeof(SamplePocoClass), maxRecursionLevel);
var structRes = gen.Generate(typeof(SampleStruct), maxRecursionLevel);
Trace.WriteLine("-------------- TEST Results ------------------------");
Trace.WriteLine(string.Format("TotalCountOfGeneratedObjects {0}", gen.TotalCountOfGeneratedObjects));
Trace.WriteLine(string.Format("Generating errors {0}", gen.Errors.Count));
}
[TestMethod]
公共void GenerateTest()
{
RPGenerator=新的RPGenerator();
int maxRecursionLevel=4;
var intRes=gen.Generate(maxRecursionLevel);
var stringArrayRes=gen.Generate(maxRecursionLevel);
var charArrayRes=gen.Generate(maxRecursionLevel);
var pocoRes=gen.Generate(maxRecursionLevel);
var structures=gen.Generate(maxRecursionLevel);
var pocoArray=gen.Generate(maxRecursionLevel);
var listRes=gen.Generate(maxRecursionLevel);
var dictRes=gen.Generate(maxRecursionLevel);
var parameterlessList=gen.Generate(maxRecursionLevel);
//非泛型生成
var stringArrayRes=gen.Generate(typeof(string[]),maxRecursionLevel;
var pocoRes=gen.Generate(typeof(SamplePocoClass),maxRecursionLevel);
var structures=gen.Generate(typeof(SampleStruct),maxRecursionLevel);
Trace.WriteLine(“--------------测试结果--------------------”;
WriteLine(string.Format(“generatedobjects{0},gen.TotalCountOfGeneratedObjects”);
WriteLine(string.Format(“生成错误{0}”,gen.errors.Count));
}
简单清洁:
public static void populateObject( object o)
{
Random r = new Random ();
FieldInfo[] propertyInfo = o.GetType().GetFields();
for (int i = 0; i < propertyInfo.Length; i++)
{
FieldInfo info = propertyInfo[i];
string strt = info.FieldType.Name;
Type t = info.FieldType;
try
{
dynamic value = null;
if (t == typeof(string) || t == typeof(String))
{
value = "asdf";
}
else if (t == typeof(Int16) || t == typeof(Int32) || t == typeof(Int64))
{
value = (Int16)r.Next(999);
info.SetValue(o, value);
}
else if (t == typeof(Int16?))
{
Int16? v = (Int16)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(Int32?))
{
Int32? v = (Int32)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(Int64?))
{
Int64? v = (Int64)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(DateTime) || t == typeof(DateTime?))
{
value = DateTime.Now;
info.SetValue(o, value);
}
else if (t == typeof(double) || t == typeof(float) || t == typeof(Double))
{
value = 17.2;
info.SetValue(o, value);
}
else if (t == typeof(char) || t == typeof(Char))
{
value = 'a';
info.SetValue(o, value);
}
else
{
//throw new NotImplementedException ("Tipo não implementado :" + t.ToString () );
object temp = info.GetValue(o);
if (temp == null)
{
temp = Activator.CreateInstance(t);
info.SetValue(o, temp);
}
populateObject(temp);
}
}
catch (Exception ex)
{
}
}
}
公共静态void populateObject(对象o)
{
Random r=新的Random();
FieldInfo[]propertyInfo=o.GetType().GetFields();
for(int i=0;i[TestMethod]
public void GenerateTest()
{
RPGenerator gen = new RPGenerator();
int maxRecursionLevel = 4;
var intRes = gen.Generate<int>(maxRecursionLevel);
var stringArrayRes = gen.Generate<string[]>(maxRecursionLevel);
var charArrayRes = gen.Generate<char[]>(maxRecursionLevel);
var pocoRes = gen.Generate<SamplePocoClass>(maxRecursionLevel);
var structRes = gen.Generate<SampleStruct>(maxRecursionLevel);
var pocoArray = gen.Generate<SamplePocoClass[]>(maxRecursionLevel);
var listRes = gen.Generate<List<SamplePocoClass>>(maxRecursionLevel);
var dictRes = gen.Generate<Dictionary<string, List<List<SamplePocoClass>>>>(maxRecursionLevel);
var parameterlessList = gen.Generate<List<Tuple<string, int>>>(maxRecursionLevel);
// Non-generic Generate
var stringArrayRes = gen.Generate(typeof(string[]), maxRecursionLevel);
var pocoRes = gen.Generate(typeof(SamplePocoClass), maxRecursionLevel);
var structRes = gen.Generate(typeof(SampleStruct), maxRecursionLevel);
Trace.WriteLine("-------------- TEST Results ------------------------");
Trace.WriteLine(string.Format("TotalCountOfGeneratedObjects {0}", gen.TotalCountOfGeneratedObjects));
Trace.WriteLine(string.Format("Generating errors {0}", gen.Errors.Count));
}
public static void populateObject( object o)
{
Random r = new Random ();
FieldInfo[] propertyInfo = o.GetType().GetFields();
for (int i = 0; i < propertyInfo.Length; i++)
{
FieldInfo info = propertyInfo[i];
string strt = info.FieldType.Name;
Type t = info.FieldType;
try
{
dynamic value = null;
if (t == typeof(string) || t == typeof(String))
{
value = "asdf";
}
else if (t == typeof(Int16) || t == typeof(Int32) || t == typeof(Int64))
{
value = (Int16)r.Next(999);
info.SetValue(o, value);
}
else if (t == typeof(Int16?))
{
Int16? v = (Int16)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(Int32?))
{
Int32? v = (Int32)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(Int64?))
{
Int64? v = (Int64)r.Next(999);
info.SetValue(o, v);
}
else if (t == typeof(DateTime) || t == typeof(DateTime?))
{
value = DateTime.Now;
info.SetValue(o, value);
}
else if (t == typeof(double) || t == typeof(float) || t == typeof(Double))
{
value = 17.2;
info.SetValue(o, value);
}
else if (t == typeof(char) || t == typeof(Char))
{
value = 'a';
info.SetValue(o, value);
}
else
{
//throw new NotImplementedException ("Tipo não implementado :" + t.ToString () );
object temp = info.GetValue(o);
if (temp == null)
{
temp = Activator.CreateInstance(t);
info.SetValue(o, temp);
}
populateObject(temp);
}
}
catch (Exception ex)
{
}
}
}