TesterHelper 1.2.0

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

TesterHelper

A powerful .NET library for creating test objects with dummy data using reflection. This library simplifies unit testing by automatically generating complex object graphs with sensible dummy values while allowing precise control over specific properties.

Features

  • Automatic dummy data generation for all primitive types and common .NET types
  • Type-safe property setting using lambda expressions
  • Private property access via reflection and backing fields
  • Nested object creation with configurable depth limits
  • Explicit null handling - distinguish between "not set" and "explicitly null"
  • Collection support (Lists, Arrays, Dictionaries)
  • Constructor parameter handling for classes without parameterless constructors
  • Enum support with automatic value selection
  • Circular reference protection with depth limiting

Basic Usage

// Create a simple object with dummy data
var person = TesterHelper.For<Person>().Create();

// Override specific properties
var customPerson = TesterHelper.For<Person>()
    .With(p => p.Name, "John Doe")
    .With(p => p.Age, 30)
    .Create();

// Explicitly set properties to null
var personWithNulls = TesterHelper.For<Person>()
    .WithNull(p => p.Address)  // Explicitly null
    .Create();

Advanced Features

Private Property Setting

TesterHelper can set private properties and backing fields using reflection:

public class SecureModel
{
    public string Name { get; set; }
    public string Secret { get; private set; }  // Private setter
    private string _hiddenValue;                // Private field
    public string HiddenValue => _hiddenValue;
}

// TesterHelper will automatically populate private properties
var model = TesterHelper.For<SecureModel>().Create();
// model.Secret and model.HiddenValue will have dummy values

Nested Objects and Collections

public class Company
{
    public string Name { get; set; }
    public List<Person> Employees { get; set; }
    public Address HeadOffice { get; set; }
}

// Creates nested objects automatically
var company = TesterHelper.For<Company>().Create();
// company.Employees will contain dummy Person objects
// company.HeadOffice will be populated with dummy address data

Depth Control

Control how deep nested object creation goes to prevent infinite recursion:

var shallowModel = TesterHelper.For<ComplexModel>()
    .WithMaxDepth(2)  // Limit nesting to 2 levels
    .Create();

Constructor Parameters

TesterHelper handles classes that only have parameterized constructors:

public class ImmutableModel
{
    public string Name { get; }
    public int Value { get; }
    
    public ImmutableModel(string name, int value)
    {
        Name = name;
        Value = value;
    }
}

// Works automatically - constructor parameters get dummy values
var model = TesterHelper.For<ImmutableModel>().Create();

Default Dummy Values

TesterHelper provides sensible defaults for common types:

Type Default Value
string "DummyString"
int 42
bool true
DateTime Jan 1, 2024
Guid NewGuid()
Enum First enum value
Collections Single-item collection with dummy element

Examples

Basic Model Creation

public class User
{
    public string Username { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    public bool IsActive { get; set; }
    public DateTime CreatedDate { get; set; }
    public List<string> Roles { get; set; }
}

// Create with all dummy data
var user = TesterHelper.For<User>().Create();

// Override specific properties
var adminUser = TesterHelper.For<User>()
    .With(u => u.Username, "admin")
    .With(u => u.Email, "admin@example.com")
    .With(u => u.Roles, new List<string> { "Admin", "User" })
    .Create();

Testing Null Scenarios

public class Order
{
    public string OrderId { get; set; }
    public Customer? Customer { get; set; }  // Nullable
    public string? Notes { get; set; }       // Nullable
}

// Test with null customer
var orderWithoutCustomer = TesterHelper.For<Order>()
    .With(o => o.OrderId, "ORD-123")
    .WithNull(o => o.Customer)  // Explicitly null
    .Create();

// Customer will be null, Notes will get dummy data
Assert.Null(orderWithoutCustomer.Customer);
Assert.NotNull(orderWithoutCustomer.Notes);

Complex Nested Scenarios

public class BlogPost
{
    public string Title { get; set; }
    public string Content { get; set; }
    public Author Author { get; set; }
    public List<Comment> Comments { get; set; }
    public Dictionary<string, string> Metadata { get; set; }
}

public class Author
{
    public string Name { get; set; }
    public string Email { get; set; }
    public Profile Profile { get; set; }
}

public class Comment
{
    public string Text { get; set; }
    public Author Author { get; set; }
    public DateTime Posted { get; set; }
}

public class Profile
{
    public string Bio { get; set; }
    public string Website { get; set; }
}

// Creates complete object graph
var blogPost = TesterHelper.For<BlogPost>()
    .With(b => b.Title, "My Test Post")
    .WithMaxDepth(3)  // Prevent too deep nesting
    .Create();

// blogPost.Author.Profile will be populated
// blogPost.Comments will contain Comment objects with nested Authors
// blogPost.Metadata will contain a dummy key-value pair

Limitations

  1. Readonly fields cannot be set after construction
  2. Static properties are not supported
  3. Complex generic constraints may require special handling
  4. Performance - Reflection has overhead (but fine for tests)

Best Practices

  1. Use in unit tests only - Not intended for production code
  2. Set depth limits for deeply nested models to prevent performance issues
  3. Override important properties explicitly rather than relying on dummy values
  4. Use explicit nulls when testing null-handling scenarios
  5. Consider immutable objects - TesterHelper handles constructor parameters automatically

Installation

Add the TesterHelper project to your solution and reference it from your test projects:

<ItemGroup>
  <ProjectReference Include="..\TesterHelper\TesterHelper.csproj" />
</ItemGroup>

Contributing

This library uses reflection extensively and handles edge cases for various .NET types. When contributing:

  1. Add tests for new type support
  2. Consider performance implications
  3. Handle nullable reference types appropriately
  4. Test with various .NET versions

Note: This library is designed specifically for unit testing scenarios where you need to quickly create test objects with realistic data. It should not be used in production code due to its reliance on reflection and potential performance implications.

Product Compatible and additional computed target framework versions.
.NET 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.
  • 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.2.0 85 8/11/2025
1.0.0 87 8/11/2025

v1.2.0: Added static TesterHelper.Create<T>() method for simplified API usage. This resolves namespace conflicts and provides a cleaner syntax for basic object creation: TesterHelper.Create<MyClass>(). The existing TesterHelper.For<T>().Create() pattern remains available for scenarios requiring property customization. All init-only property support from v1.1.0 is preserved.