TesterHelper 1.2.0
dotnet add package TesterHelper --version 1.2.0
NuGet\Install-Package TesterHelper -Version 1.2.0
<PackageReference Include="TesterHelper" Version="1.2.0" />
<PackageVersion Include="TesterHelper" Version="1.2.0" />
<PackageReference Include="TesterHelper" />
paket add TesterHelper --version 1.2.0
#r "nuget: TesterHelper, 1.2.0"
#:package TesterHelper@1.2.0
#addin nuget:?package=TesterHelper&version=1.2.0
#tool nuget:?package=TesterHelper&version=1.2.0
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
- Readonly fields cannot be set after construction
- Static properties are not supported
- Complex generic constraints may require special handling
- Performance - Reflection has overhead (but fine for tests)
Best Practices
- Use in unit tests only - Not intended for production code
- Set depth limits for deeply nested models to prevent performance issues
- Override important properties explicitly rather than relying on dummy values
- Use explicit nulls when testing null-handling scenarios
- 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:
- Add tests for new type support
- Consider performance implications
- Handle nullable reference types appropriately
- 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 | Versions 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. |
-
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.
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.