Cayd.Test.Generators 2.0.2

dotnet add package Cayd.Test.Generators --version 2.0.2
                    
NuGet\Install-Package Cayd.Test.Generators -Version 2.0.2
                    
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="Cayd.Test.Generators" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cayd.Test.Generators" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Cayd.Test.Generators" />
                    
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 Cayd.Test.Generators --version 2.0.2
                    
#r "nuget: Cayd.Test.Generators, 2.0.2"
                    
#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.
#addin nuget:?package=Cayd.Test.Generators&version=2.0.2
                    
Install Cayd.Test.Generators as a Cake Addin
#tool nuget:?package=Cayd.Test.Generators&version=2.0.2
                    
Install Cayd.Test.Generators as a Cake Tool

About

This package includes utilities such as StringGenerator, CreditCardNumberGenerator, EmailGenerator, ClassGenerator, EnumerableGenerator, EnumGenerator and many more to help you generate values, collections and classes for your test code.

How to Use

After installing the package and adding Cayd.Test.Generators namespace to your test code, you can start using the static utility classes to generate values, collections or classes.

A few example of how to use the library:

using Cayd.Test.Generators;

public class MyEmailCheckerTest
{
    // ... setup other needs

    [Fact]
    public void Check_WhenEmailIsValid_ShouldReturnTrue()
    {
        // Arrange
        var email = EmailGenerator.Generate(); // Returns a valid random email address.

        // Act
        var result = MyEmailChecker.Check(email);

        // Assert
        Assert.True(result);
    }
}

Or when you have an entity class(es) to be generated:

public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }

    public ICollection<Book> Books { get; set; }
}

public class Book
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public EStatus Status { get; set; }
    public DateTime CreatedDate { get; set; }

    // This would normally cause infinite recursive calls, however, ClassGenerator can detect them at one level of depth. This variable will be set to null
    public Author Author { get; set; }
}

public enum EStatus
{
    Discontinued = -100,
    Undefined = 0,
    Available = 1,
    OutOfStock = 2
}

// You can simple call Generate method in ClassGenerator and the instance will be populated with values
Author testAuthor = ClassGenerator.Generate<Author>();

If you want to set your own generator for specific properties or not to generate values for them, you can defined them as follows:

/** For .Net 8 and above */
Author testAuthor = ClassGenerator.Generate<Author>([
    (a => a.Id, 5),                          // You can give exact values for properties instead of random values
    (a => a.Books, () => new List<Book>())   // For class types, you can give exact values using anonymous functions
    (a => a.Name, MyOwnStringGenerator),     // You can use your own value generators for properties
    (a => a.DateOfBirth, () => {             // You can define your own value generator using anonymous functions
        var dateTime = DateTime.UtcNow;
        // ... do some calculations
        return dateTime;
    })
]);

/** For versions lower than .Net 8 */
Author testAuthor = ClassGenerator.Generate(new (Expression<Func<Author, object?>>, Func<object?>)[] {
    (a => a.Id, 5),                          // You can give exact value for properties instead of random values
    (a => a.Books, () => new List<Book>())   // For class types, you can give exact values using anonymous functions
    (a => a.Name, MyOwnStringGenerator),     // You can use your own value generators for properties
    (a => a.DateOfBirth, () => {             // You can define your own value generator using anonymous functions
        var dateTime = DateTime.UtcNow;
        // ... do some calculations
        return dateTime;
    })
});

Generators

The package includes the following static utility classes:

  • ClassGenerator
  • CreditCardNumberGenerator
  • DateTimeGenerator
  • DictionaryGenerator
  • EmailGenerator
  • EnumerableGenerator
  • EnumGenerator
  • GuidGenerator
  • IpAddressGenerator
  • PasswordGenerator
  • PhoneNumberGenerator
  • StringGenerator
  • TimeSpanGenerator

To learn more about their usages in detail, you can check the GitHub Wiki.

Limitations

The current version has the following limitations when it comes to ClassGenerator.

  • ClassGenerator generates values only for properties of a class. For other types, they need to be assigned manually.
  • ClassGenerator does not populate struct types with values but initialize them with their default values, except Guid, DateTime and TimeSpan. They are generated automatically.
  • The expression parameter in the Generate method of ClassGenerator does not support more than 1 depth. For instance, this expression will not work: x => x.Property1.Property2. Only expressions like x => x.Property1 are supported.
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.

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
2.0.2 126 6/1/2025
2.0.1 143 4/29/2025
2.0.0 130 4/27/2025
1.1.0 177 4/15/2025
1.0.0 208 4/14/2025

Update package info