Ahatornn.TestGenerator 1.1.29

dotnet add package Ahatornn.TestGenerator --version 1.1.29
                    
NuGet\Install-Package Ahatornn.TestGenerator -Version 1.1.29
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ahatornn.TestGenerator" Version="1.1.29" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ahatornn.TestGenerator" Version="1.1.29" />
                    
Directory.Packages.props
<PackageReference Include="Ahatornn.TestGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ahatornn.TestGenerator --version 1.1.29
                    
#r "nuget: Ahatornn.TestGenerator, 1.1.29"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ahatornn.TestGenerator@1.1.29
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ahatornn.TestGenerator&version=1.1.29
                    
Install as a Cake Addin
#tool nuget:?package=Ahatornn.TestGenerator&version=1.1.29
                    
Install as a Cake Tool

TestGenerator

Data generator for test projects

Every time you write tests, you have to initialize data to test specific cases of your services. To separate models with default values from prepared models, use a managed data generator. This will allow you to verify the correct behavior of the business logic when checking tests and get rid of the unwanted effects of default behavior for empty models in the test.

Use TestEntityProvider.Shared.Create<> for creating test entity. For example:

var actCarrier1 = TestEntityProvider.Shared.Create<ActCarrier>();
var actCarrierOrder1 = TestEntityProvider.Shared.Create<ActCarrierOrder>(x => x.ActCarrierId = actCarrier.Id);
var actCarrierOrder2 = TestEntityProvider.Shared.Create<ActCarrierOrder>(x => 
{
  x.ActCarrierId = actCarrierDeleted.Id;
  x.ExternalOrderId = orderId;
});

Method Create<> using instead

var actCarrier1 = new ActCarrier
{
    Id = Guid.NewGuid(),
    Currency = $"Currency{Guid.NewGuid():N}",
    DocumentDate = DateTimeOffset.UtcNow,
    DocumentNumber = $"DocumentNumber{Guid.NewGuid():N}",
    ExternalContractId = Guid.NewGuid(),
    ExternalContractorId = Guid.NewGuid(),
    ExternalId = Guid.NewGuid(),
    SourceUpdatedAt = DateTimeOffset.UtcNow,
};

By default, TestEntityProvider generates data for the following property types (if they are writable)

  • String: $"{propertyInfo.Name}{Guid.NewGuid():N}"
  • DateTime: DateTime.Now
  • DateTimeOffset: DateTimeOffset.Now
  • Guid: Guid.NewGuid()

You can expand the list of available data generators by implementing the interface IPropertyValueGenerator. However, in this case you will need to use the TestEntityProvider instance obtained using the TestEntityProviderBuilder, rather than using TestEntityProvider.Shared. Let's see how to implement a data generator for all boolean types.

First, let's implement IPropertyValueGenerator for the boolean type

public class BoolAlwaysTrueValueGenerator : IPropertyValueGenerator
{
  Type IPropertyValueGenerator.PropertyValueType => typeof(bool);

  void IPropertyValueGenerator.Generate<TEntity>([NotNull] TEntity entity, [NotNull] PropertyInfo propertyInfo)
    where TEntity : class
  {
    if (!(propertyInfo.CanWrite && propertyInfo.PropertyType == typeof(bool)))
    {
      throw new InvalidOperationException($"The property {propertyInfo.Name} could not be write for {GetType().Name}");
    }

    propertyInfo.SetValue(entity, true);
  }
}

Now let's create an TestEntityProviderBuilder by passing a custom IPropertyValueGenerator

var provider = new TestEntityProviderBuilder
    .AddGenerator(new BoolAlwaysTrueValueGenerator())
    .Build();

And finally use provider to generate data

var result = provider.Create<SimpleTestModel>();

If you regularly need to set the same values for certain properties when using TestEntityProvider, use the AddPreset mechanism. Just specify the necessary values for properties of a certain type when creating the TestEntityProvider

var provider = new TestEntityProviderBuilder
  .AddPreset<SimpleTestModel>(x => x.Cost = 11.7m)
  .Build();

Now every time you call provider.Create<SimpleTestModel>() you will receive a value of 11.7m for the Cost property of each model obtained using TestEntityProvider. Of course, if necessary, you can override the previously specified value provider.Create<SimpleTestModel>(x => x.Cost = 8.3m)

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.29 1,426 3/12/2025
1.1.28 231 3/11/2025
1.1.26 273 3/11/2025
1.1.25 250 3/11/2025
1.0.18 1,358 2/18/2024

v 1.1.29:
 - TargetFrameworks: net8.0, net6.0
 v 1.1.28:
 - Now MIT license
 v 1.1.25:
 - Use TestEntityProviderBuilder for add some Presets or Generators
 v 1.0.18:
 - Created shared TestEntityProvider
Created shared TestEntityProvider