Skip to content

mazensakr05/TestWire

Repository files navigation

🔌 TestWire

NuGet License: MIT .NET


I built TestWire because once I finish a controller and need to write tests, if they're straightforward I just get bored writing them. I needed something that gives me a template based on my actual controller — not generic stubs, but ones shaped around my specific endpoints and dependencies. So TestWire reads your controller and generates test stubs based on it. You still write the real logic, it just handles the part that's purely mechanical.


What Gets Generated

For every controller TestWire finds, it produces a test file with:

  • Happy path — a 200 OK / 201 Created test with a valid request
  • Bad path — a 400 BadRequest test with invalid inputs
  • Auth test — a 401 Unauthorized test when [Authorize] is present
  • Mocked constructor dependencies — every injected interface becomes a Mock<T> via Moq
  • DTO stubs with real property values — generated from your actual models, not placeholders
// Your controller (SampleApi/Controllers/ProductsController.cs)
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IProductService _service;

    public ProductsController(IProductService service)
    {
        _service = service;
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(int id) { ... }

    [HttpPost]
    [Authorize]
    public async Task<IActionResult> Create([FromBody] CreateProductDto dto) { ... }
}
// Generated by TestWire
public class ProductsControllerTests
{
    private readonly Mock<IProductService> _mockService = new();
    private readonly ProductsController _sut;

    public ProductsControllerTests()
    {
        _sut = new ProductsController(_mockService.Object);
    }

    [Fact]
    public async Task GetById_ReturnsOk_WhenProductExists()
    {
        var result = await _sut.GetById(1);
        result.Should().BeOfType<OkObjectResult>();
    }

    [Fact]
    public async Task GetById_ReturnsBadRequest_WhenIdIsInvalid()
    {
        var result = await _sut.GetById(-1);
        result.Should().BeOfType<BadRequestObjectResult>();
    }

    [Fact]
    public async Task Create_ReturnsCreated_WhenRequestIsValid()
    {
        var dto = new CreateProductDto { Name = "TestName", Price = 1 };
        var result = await _sut.Create(dto);
        result.Should().BeOfType<CreatedAtActionResult>();
    }

    [Fact]
    public async Task Create_ReturnsUnauthorized_WhenUserIsNotAuthenticated()
    {
        var result = await _sut.Create(new CreateProductDto());
        result.Should().BeOfType<UnauthorizedResult>();
    }
}

Installation

Requires .NET 8+

dotnet tool install -g testwire

Usage

Basic

testwire generate --project ./MyApi/MyApi.csproj

Scans your project and writes test files to ./TestWire.Generated/ by default.

Custom output directory

testwire generate --project ./MyApi/MyApi.csproj --output ./tests

Preview without writing files

testwire generate --project ./MyApi/MyApi.csproj --dry-run

Use NUnit instead of xUnit

testwire generate --project ./MyApi/MyApi.csproj --framework nunit

All Options

Flag Description Default
--project Path to the .csproj file (required)
--output Directory to write generated test files ./TestWire.Generated
--framework Test framework: xunit or nunit xunit
--dry-run Print output to console, don't write files false

Changelog

See CHANGELOG.md for the full history of changes.


Current Limitations (v0.1.2)

This is an early release. Here's what's not supported yet:

  • [FromQuery] and [FromHeader] parameters are not yet analyzed
  • No .csproj file is generated for the output test folder — you need to add one manually
  • No --controller flag to target a single controller
  • Mock .Setup(...) uses default behavior — explicit return values coming in v0.2

If any of these block you, please open an issue.


Contributing

PRs and issues are welcome. Open an issue to discuss what you'd like to change before submitting a PR.


License

MIT © Mazen Hassan

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages