testgen 0.3.0

dotnet tool install --global testgen --version 0.3.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local testgen --version 0.3.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=testgen&version=0.3.0
                    
nuke :add-package testgen --version 0.3.0
                    

testgen

CLI tool for generating hash-based unit tests for C# models.

testgen eliminates repetitive boilerplate when testing hash-based constructors by generating deterministic and exhaustive test cases for all parameter combinations.


โœจ Features

  • Generates [Fact] tests for all constructor combinations
  • Supports ladder-style test progression (one hash at a time)
  • Works via CLI or JSON configuration
  • Outputs .cs test files
  • Designed for hash-driven domain models

๐Ÿ“ฆ Installation

As a global .NET tool

dotnet tool install --global testgen

Local (from src folder)

dotnet pack -c Release
dotnet tool install --global --add-source bin/Release testgen

๐Ÿš€ Quick Start

1. Generate config

testgen init

Creates:

testgen.json

2. Generate tests

testgen --config=testgen.json

Output:

MyModelHashTests.cs

๐Ÿ–ฅ CLI Usage

testgen --name=MyModel --interface=IMyModel --hash=MyModelHash --param="id:IGuid:new Guid():new DeterminedHash(id)" --param="chartId:IGuid:new Guid():new DeterminedHash(chartId)" --out=MyModelHashTests.cs

โš ๏ธ Passing Parameters via CLI

Each parameter must follow this format:

name:type:init:hashExpr

Example:

--param="id:IGuid:new Guid():new DeterminedHash(id)"

โ— Important: spaces in expressions

Shell splits arguments by spaces. If your expressions contain spaces (e.g. new Guid()), you must wrap the entire parameter in quotes.

โœ… Correct

--param="id:IGuid:new Guid():new DeterminedHash(id)"

โŒ Incorrect

--param=id:IGuid:new Guid():new DeterminedHash(id)

This will be parsed incorrectly as multiple arguments.


๐Ÿ’ก Tip

For complex models, prefer JSON config:

testgen --config=testgen.json

It is more readable and less error-prone.


โš™๏ธ Configuration

Example testgen.json:

{
  "ModelName": "MyModel",
  "ModelHash": "MyModelHash",
  "ModelInterface": "IMyModel",
  "Params": [
    {
      "Type": "IGuid",
      "Name": "id",
      "Init": "new Guid()",
      "HashExpr": "new Hash(id)"
    },
    {
      "Type": "IString",
      "Name": "name",
      "Init": "new String()",
      "HashExpr": "new Hash(name)"
    }
  ]
}

๐Ÿงช Generated Output

Value-based test

[Fact]
public void ProduceCorrectHashFromValues()
{
    IGuid id = new Guid();
    IGuid chartId = new Guid();

    IMyModel model = new MyModel(
        id,
        chartId
    );

    MyModelHash expected = new MyModelHash(model);
    MyModelHash actual = new MyModelHash(
        id,
        chartId
    );

    Assert.True(expected.SequenceEqual(actual));
}

Hash-based test

[Fact]
public void ProduceCorrectHashFromIdHash()
{
    IGuid id = new Guid();
    IGuid chartId = new Guid();

    IMyModel model = new MyModel(
        id,
        chartId
    );

    MyModelHash expected = new MyModelHash(model);
    MyModelHash actual = new MyModelHash(
        new DeterminedHash(id),
        chartId
    );

    Assert.True(expected.SequenceEqual(actual));
}

๐Ÿง  How It Works

For a model with N parameters, testgen:

  1. Builds a model using raw values
  2. Generates expected hash via model constructor
  3. Iterates through parameter combinations
  4. Replaces values with hashes according to mask
  5. Verifies equality using SequenceEqual

Tests are sorted by number of hash parameters for better readability.


๐Ÿ“Š Before / After

โŒ Manual approach

  • Write dozens of repetitive tests
  • Easy to miss combinations
  • Hard to maintain

โœ… With testgen

testgen --config=testgen.json
  • All combinations generated automatically
  • Deterministic and consistent
  • Minimal maintenance

๐Ÿ“‹ Arguments

Argument Description
--name Model class name
--hash Hash class name (optional)
--interface Model interface (optional)
--param Parameter definition (name:type:init:hashExpr, wrap in quotes if needed)
--config Path to JSON config
--out Output file (optional)
--help, -h Show help

๐Ÿ“ Examples

See /examples:

examples/
โ”œโ”€โ”€ testgen.json
โ””โ”€โ”€ generated-tests.cs

๐Ÿ“„ License

MIT License


๐Ÿค Contributing

Contributions are welcome.

If you find a bug or want a feature โ€” open an issue or submit a PR.


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 is compatible.  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 is compatible.  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.

This package has no dependencies.

Version Downloads Last Updated
0.3.0 99 4/25/2026
0.2.0 108 4/21/2026
0.1.0 102 4/18/2026