NUnit Testing
БесплатноБез исполняемых скриптовНе проверенNUnit 3 constraint-based testing for C# covering Assert.That patterns, parameterized tests, setup/teardown, Moq mocking, test categories, and the fluent…
от PramodDutta★ 253 звезды на GitHubОб этом скилле
NUnit Testing Skill
You are an expert C# developer specializing in testing with NUnit 3. When the user asks you to write, review, or debug NUnit tests, follow these detailed instructions to produce robust test suites that leverage NUnit's constraint-based assertion model and powerful parameterization features.
Core Principles
- Test behavior, not implementation -- Verify what the code does from a caller's perspective rather than internal implementation details.
- Use the constraint model -- Prefer
Assert.That(actual, Is.EqualTo(expected))over classicAssert.AreEqualfor readable, composable assertions. - One logical assertion per test -- Each
[Test]method should verify a single behavior for precise failure diagnosis. - Arrange-Act-Assert -- Structure every test into setup, execution, and verification sections for clarity.
- Isolate external dependencies -- Use Moq to mock databases, HTTP clients, and third-party services in unit tests.
- Descriptive test names -- Name tests as
MethodName_Scenario_ExpectedResultso test output reads as a specification. - Leverage parameterized tests -- Use
[TestCase]and[TestCaseSource]to test multiple inputs without code duplication.
Project Structure
Solution/
src/
MyApp/
Services/
UserService.cs
PaymentService.cs
Models/
User.cs
Order.cs
Repositories/
IUserRepository.cs
UserRepository.cs
Utilities/
Validators.cs
tests/
MyApp.Tests/
Services/
UserServiceTests.cs
PaymentServiceTests.cs
Models/
UserTests.cs
OrderTests.cs
Utilities/
ValidatorsTests.cs
Fixtures/
TestDataFactory.cs
MyApp.Tests.csproj
MyApp.IntegrationTests/
UserPaymentFlowTests.cs
MyApp.IntegrationTests.csproj
Solution.sln
Dependencies
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MyApp\MyApp.csproj" />
</ItemGroup>
</Project>
Running Tests
# Run all tests
dotnet test
# Run specific project
dotnet test tests/MyApp.Tests
# Run with filter
dotnet test --filter "FullyQualifiedName~UserServiceTests"
# Run specific category
dotnet test --filter "TestCategory=Unit"
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"
# Verbose output
dotnet test --verbosity detailed
Basic Test Structure
using NUnit.Framework;
namespace MyApp.Tests.Services;
[TestFixture]
public class UserServiceTests
{
private UserService _userService = null!;
private InMemoryUserRepository _userRepository = null!;
[SetUp]
public void SetUp()
{
_userRepository = new InMemoryUserRepository();
_userService = new UserService(_userRepository);
}
[TearDown]
public void TearDown()
{
_userRepository = null!;
_userService = null!;
}
[Test]
[Category("Unit")]
public void CreateUser_WithValidData_ReturnsUser()
{
// Arrange
var request = new CreateUserRequest("Alice", "[email protected]", 30);
// Act
var user = _userService.CreateUser(request);
// Assert
Assert.That(user, Is.Not.Null);
Assert.That(user.Name, Is.EqualTo("Alice"));
Assert.That(user.Email, Is.EqualTo("[email protected]"));
}
[Test]
[Category("Unit")]
public void CreateUser_WithoutEmail_ThrowsArgumentException()
{
var request = new CreateUserRequest("Bob", null!, 25);
var exception = Assert.Throws<ArgumentException>(
() => _userService.CreateUser(request));
Assert.That(exception!.Message, Does.Contain("email"));
}
[Test]
[Category("Unit")]
public void CreateUser_WithDuplicateEmail_ThrowsDuplicateEmailException()
{
var request = new CreateUserRequest("Alice", "[email protected]", 30);
_userService.CreateUser(request);
Assert.Throws<DuplicateEmailException>(
() => _userService.CreateUser(request));
}
}
Constraint Model Reference
[TestFixture]
public class ConstraintModelExamples
{
[Test]
public void EqualityConstraints()
{
Assert.That(2 + 2, Is.EqualTo(4));
Assert.That(2 + 2, Is.Not.EqualTo(5));
Assert.That(0.1 + 0.2, Is.EqualTo(0.3).Within(0.001));
Assert.That("Hello", Is.EqualTo("hello").IgnoreCase);
}
[Test]
Установить NUnit Testing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл NUnit Testing?
NUnit 3 constraint-based testing for C# covering Assert.That patterns, parameterized tests, setup/teardown, Moq mocking, test categories, and the fluent assertion model.
Как установить скилл NUnit Testing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл NUnit Testing запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic