agentleFS
shoaibmalickshoaibmalick/development-claude-skills/skills/testing/SKILL.md

testing

Skill0 starsChanged 11 months agoMITView on GitHub

Testing workflows for .NET with xUnit, Moq, and FluentAssertions. Use this skill when writing unit tests, integration tests, mocking dependencies, or setting up test projects. Triggers on tasks involving xUnit, test creation, mocking, assertions, or test-driven development in .NET.

194 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use bundle-dependency-prs, update-library-template and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • MicrosoftSkill

    bundle-dependency-prs

    Fix broken dependency update PRs and aggregate the ones that work into one PR.

    Also in 8 projects that use this

  • MicrosoftSkill

    update-library-template

    Merges the latest Library.Template into this repo (at position of HEAD) and resolves conflicts.

    Also in 8 projects that use this

  • NVIDIA-NeMoSkill

    build-and-dependency

    Dev environment setup for Megatron Bridge — container-based development, uv package management, lockfile regeneration, adding dependencies, Slurm container usage, and common build pitfalls.

    Also in 3 projects that use this

  • timmo001Skill

    changeset-scope

    Keep all scoped code work contained to the user-defined changeset. Use for implementation, fixes, diagnosis, refactoring, cleanup, and review when explicit instructions, named files, diffs, branches, pull requests, or injected work scopes define the boundary.

    Also in 3 projects that use this

  • JetBrainsSkill

    module-dependencies

    Add or modify IntelliJ module dependencies in `.iml` files.

    Also in 3 projects that use this

  • CES-LtdSkill

    oss-pr

    Use when creating a pull request, after committing changes, or when user invokes /oss-pr. Covers branch management, quality checks, commit, push, and PR creation.

    Also in 3 projects that use this

The file

Files
SKILL.md

Testing

A comprehensive skill for .NET testing with xUnit.

Tech Stack

CategoryTechnology
FrameworkxUnit
MockingMoq
AssertionsFluentAssertions
CoverageCoverlet

Project Structure

tests/
  MyApp.UnitTests/
    Controllers/
    Services/
    Repositories/
  MyApp.IntegrationTests/
    Api/
    Database/

Setup

Install Packages

dotnet add package xunit
dotnet add package xunit.runner.visualstudio
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package Moq
dotnet add package FluentAssertions
dotnet add package coverlet.collector

Basic Test Structure

using Xunit;

namespace MyApp.UnitTests.Services;

public class ProductServiceTests
{
    [Fact]
    public void GetById_WithValidId_ReturnsProduct()
    {
        // Arrange
        var service = new ProductService();

        // Act
        var result = service.GetById(1);

        // Assert
        Assert.NotNull(result);
        Assert.Equal(1, result.Id);
    }

    [Fact]
    public void GetById_WithInvalidId_ReturnsNull()
    {
        // Arrange
        var service = new ProductService();

        // Act
        var result = service.GetById(-1);

        // Assert
        Assert.Null(result);
    }
}

Parameterized Tests

public class CalculatorTests
{
    [Theory]
    [InlineData(2, 3, 5)]
    [InlineData(0, 0, 0)]
    [InlineData(-1, 1, 0)]
    public void Add_ReturnsCorrectSum(int a, int b, int expected)
    {
        var calculator = new Calculator();
        var result = calculator.Add(a, b);
        Assert.Equal(expected, result);
    }

    [Theory]
    [MemberData(nameof(GetTestData))]
    public void Multiply_ReturnsCorrectProduct(int a, int b, int expected)
    {
        var calculator = new Calculator();
        var result = calculator.Multiply(a, b);
        Assert.Equal(expected, result);
    }

    public static IEnumerable<object[]> GetTestData()
    {
        yield return new object[] { 2, 3, 6 };
        yield return new object[] { 0, 5, 0 };
        yield return new object[] { -2, 3, -6 };
    }
}

Mocking with Moq

using Moq;

public class ProductServiceTests
{
    private readonly Mock<IProductRepository> _mockRepository;
    private readonly ProductService _service;

    public ProductServiceTests()
    {
        _mockRepository = new Mock<IProductRepository>();
        _service = new ProductService(_mockRepository.Object);
    }

    [Fact]
    public async Task GetAllAsync_ReturnsAllProducts()
    {
        // Arrange
        var products = new List<Product>
        {
            new Product { Id = 1, Name = "Product 1" },
            new Product { Id = 2, Name = "Product 2" }
        };
        _mockRepository.Setup(r => r.GetAllAsync())
            .ReturnsAsync(products);

        // Act
        var result = await _service.GetAllAsync();

        // Assert
        Assert.Equal(2, result.Count());
        _mockRepository.Verify(r => r.GetAllAsync(), Times.Once);
    }

    [Fact]
    public async Task CreateAsync_CallsRepository()
    {
        // Arrange
        var dto = new CreateProductDto("New Product", 99.99m);
        _mockRepository.Setup(r => r.AddAsync(It.IsAny<Product>()))
            .Returns(Task.CompletedTask);

        // Act
        await _service.CreateAsync(dto);

        // Assert
        _mockRepository.Verify(r => r.AddAsync(
            It.Is<Product>(p => p.Name == "New Product")), Times.Once);
    }
}

FluentAssertions

using FluentAssertions;

public class ProductServiceTests
{
    [Fact]
    public void GetById_WithValidId_ReturnsCorrectProduct()
    {
        // Arrange
        var service = new ProductService();

        // Act
        var result = service.GetById(1);

        // Assert
        result.Should().NotBeNull();
        result.Id.Should().Be(1);
        result.Name.Should().NotBeNullOrEmpty();
        result.Price.Should().BeGreaterThan(0);
    }

    [Fact]
    public void GetAll_ReturnsNonEmptyList()
    {
        var service = new ProductService();
        var result = service.GetAll();

        result.Should().NotBeEmpty()
            .And.HaveCountGreaterThan(0)
            .And.OnlyContain(p => p.Id > 0);
    }

    [Fact]
    public void Create_ThrowsException_WhenNameIsEmpty()
    {
        var service = new ProductService();

        var act = () => service.Create(new CreateProductDto("", 10));

        act.Should().Throw<ValidationException>()
            .WithMessage("*Name*required*");
    }
}

Async Testing

public class AsyncServiceTests
{
    [Fact]
    public async Task GetDataAsync_ReturnsData()
    {
        // Arrange
        var service = new DataService();

        // Act
        var result = await service.GetDataAsync();

        // Assert
        result.Should().NotBeNull();
    }

    [Fact]
    public async Task GetDataAsync_ThrowsException_WhenTimeout()
    {
        var service = new DataService();

        await Assert.ThrowsAsync<TimeoutException>(
            () => service.GetDataAsync(timeout: 1));
    }
}

Test Fixtures (Shared Context)

public class DatabaseFixture : IDisposable
{
    public AppDbContext Context { get; }

    public DatabaseFixture()
    {
        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseInMemoryDatabase("TestDb")
            .Options;
        Context = new AppDbContext(options);
        SeedData();
    }

    private void SeedData()
    {
        Context.Products.AddRange(
            new Product { Id = 1, Name = "Test Product" }
        );
        Context.SaveChanges();
    }

    public void Dispose()
    {
        Context.Dispose();
    }
}

public class ProductRepositoryTests : IClassFixture<DatabaseFixture>
{
    private readonly DatabaseFixture _fixture;

    public ProductRepositoryTests(DatabaseFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public async Task GetAll_ReturnsSeededProducts()
    {
        var repository = new ProductRepository(_fixture.Context);
        var result = await repository.GetAllAsync();
        result.Should().NotBeEmpty();
    }
}

Common Commands

# Run all tests
dotnet test

# Run with coverage
dotnet test --collect:"XPlat Code Coverage"

# Run specific test project
dotnet test tests/MyApp.UnitTests

# Run specific test class
dotnet test --filter "FullyQualifiedName~ProductServiceTests"

# Run specific test method
dotnet test --filter "FullyQualifiedName~GetById_WithValidId"

# Verbose output
dotnet test --logger "console;verbosity=detailed"

References

For detailed guides, see:

  • references/mocking-guide.md - Advanced mocking patterns
  • references/integration-tests.md - Integration testing patterns
  • references/test-patterns.md - Common testing patterns

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.