Skip to content
akovanev edited this page Dec 5, 2022 · 24 revisions

Setup

Install Akov.DataGenerator NuGet package.

dotnet add package Akov.DataGenerator

Changelog

Usage

Akov.DataGenerator library helps to generate random data based on its internal DataScheme.

There are three main ways how the DataScheme can be obtained.

The DG output for the first two can then be represented as a json string or a generic object or collection.

// Get DataScheme
DataScheme GetFromFile(string filename)
DataScheme GetFromType<T>()

// Generate data
string GenerateJson(DataScheme scheme)
string GenerateJsonCollection(DataScheme scheme, int count)
T GenerateObject<T>(DataScheme scheme)
IEnumerable<T> GenerateObjectCollection<T>(DataScheme scheme, int count)

When dealing with profiles there is no public method populating the DataScheme. Instead you can directly get the output.

string GenerateJson<T>(DgProfileBase profile)
string GenerateJsonCollection<T>(DgProfileBase profile, int count)
T GenerateObject<T>(DgProfileBase profile)
IEnumerable<T> GenerateObjectCollection<T>(DgProfileBase profile, int count)

Use last run results

This option can be useful if a test failed and you would like to run it again against the same data.

var runBehavior = new StoreToFileRunBehavior();
var dg = new DG(new StudentGeneratorFactory(), runBehavior: runBehavior);
dg.GenerateJson<StudentCollection>(profile!, useLast: true);
// Clear data when not needed anymore
runBehavior.ClearResult(nameof(StudentCollection)); 

What should you be aware of?

At this moment, object generation is literally deserialization of the built json.

Json first approach was chosen to introduce Random Failure feature. If it is enabled, then the generated json is supposed to be invalid/incorrect for some objects, and deserialization may fail unless specific error handling is added. Like in this example based on Newtonsoft.Json usage. Unfortunately, System.Text.Json doesn't support this behavior yet.

Clone this wiki locally