StrangerData is a tool designed to automatically fills your database with random data to make your unit/integration tests faster.
The generator will auto maps all foreign keys and generates records to related tables.
- Install StrangerData with NuGet Package Manager:
Install-Package StrangerData
- Install your database dialect, example:
Install-Package StrangerData.SqlServer
- Configure the required connection strings.
To start generating your test data, create a new DataFactory object:
using StrangerData;
using StrangerData.SqlServer;
...
var dataFactory = new DataFactory<SqlServerDialect>("MyConnectionString");
Consider the example schema:
Column | Data Type | PK | FK |
---|---|---|---|
Id | INT | True | False |
Name | VARCHAR(20) | False | False |
VARCHAR(50) | False | False | |
Age | INT | False | False |
TeamId | INT | False | Team(Id) |
Column | Data Type | PK | FK |
---|---|---|---|
Id | INT | True | False |
Name | VARCHAR(20) | False | False |
...
IDicionary<string, object> record = dataFactory.CreateOne("dbo.Person");
The method will creates an record in the group table, and associates it to the created user. The dictionary will contains:
{
"Id": "Generated User's Id",
"Name": "Random string",
"Email": "Random string",
"Age": "Random integer number",
"TeamId": "Id from generated group record"
}
So you can specify your custom values. Do following:
User user = dataFactory.CreateOne("dbo.Person", t => {
t.WithValue("Name", "Will Byers");
});
The dictionary will contains:
{
"Id": "Generated User's Id",
"Name": "Will Byers",
"Email": "Random string",
"Age": "Random integer number",
"TeamId": "Id from generated group record"
}
To delete all generated records, just run:
...
dataFactory.TearDown();
...
We suggest to use the TearDown() method inside yor Finally scope. This way it will run even if you code crashes on running, avoiding to have dirty data on your database.