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
<PackageReference Include="Cayd.Test.Generators" Version="2.0.2" />
<PackageVersion Include="Cayd.Test.Generators" Version="2.0.2" />
<PackageReference Include="Cayd.Test.Generators" />
paket add Cayd.Test.Generators --version 2.0.2
#r "nuget: Cayd.Test.Generators, 2.0.2"
#addin nuget:?package=Cayd.Test.Generators&version=2.0.2
#tool nuget:?package=Cayd.Test.Generators&version=2.0.2
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 populatestruct
types with values but initialize them with their default values, exceptGuid
,DateTime
andTimeSpan
. They are generated automatically.- The expression parameter in the
Generate
method ofClassGenerator
does not support more than 1 depth. For instance, this expression will not work:x => x.Property1.Property2
. Only expressions likex => x.Property1
are supported.
Product | Versions 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. |
-
net6.0
- Cayd.Random.Extensions (>= 1.0.4)
-
net8.0
- Cayd.Random.Extensions (>= 1.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Update package info