csharp-nunit
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNUnit Best Practices
NUnit单元测试最佳实践
Your goal is to help me write effective unit tests with NUnit, covering both standard and data-driven testing approaches.
你的目标是帮助我编写高效的NUnit单元测试,涵盖标准测试和数据驱动测试两种方法。
Project Setup
项目设置
- Use a separate test project with naming convention
[ProjectName].Tests - Reference Microsoft.NET.Test.Sdk, NUnit, and NUnit3TestAdapter packages
- Create test classes that match the classes being tested (e.g., for
CalculatorTests)Calculator - Use .NET SDK test commands: for running tests
dotnet test
- 使用单独的测试项目,命名规范为
[项目名称].Tests - 引用Microsoft.NET.Test.Sdk、NUnit和NUnit3TestAdapter包
- 创建与被测类对应的测试类(例如,为创建
Calculator)CalculatorTests - 使用.NET SDK测试命令:来运行测试
dotnet test
Test Structure
测试结构
- Apply attribute to test classes
[TestFixture] - Use attribute for test methods
[Test] - Follow the Arrange-Act-Assert (AAA) pattern
- Name tests using the pattern
MethodName_Scenario_ExpectedBehavior - Use and
[SetUp]for per-test setup and teardown[TearDown] - Use and
[OneTimeSetUp]for per-class setup and teardown[OneTimeTearDown] - Use for assembly-level setup and teardown
[SetUpFixture]
- 为测试类应用特性
[TestFixture] - 为测试方法使用特性
[Test] - 遵循Arrange-Act-Assert(AAA)模式
- 使用的模式命名测试
方法名_场景_预期行为 - 使用和
[SetUp]进行每个测试的初始化和清理[TearDown] - 使用和
[OneTimeSetUp]进行每个测试类的初始化和清理[OneTimeTearDown] - 使用进行程序集级别的初始化和清理
[SetUpFixture]
Standard Tests
标准测试
- Keep tests focused on a single behavior
- Avoid testing multiple behaviors in one test method
- Use clear assertions that express intent
- Include only the assertions needed to verify the test case
- Make tests independent and idempotent (can run in any order)
- Avoid test interdependencies
- 保持测试专注于单一行为
- 避免在一个测试方法中测试多个行为
- 使用清晰的断言来表达意图
- 只包含验证测试用例所需的断言
- 确保测试独立且幂等(可以按任意顺序运行)
- 避免测试之间的依赖
Data-Driven Tests
数据驱动测试
- Use for inline test data
[TestCase] - Use for programmatically generated test data
[TestCaseSource] - Use for simple parameter combinations
[Values] - Use for property or method-based data sources
[ValueSource] - Use for random numeric test values
[Random] - Use for sequential numeric test values
[Range] - Use or
[Combinatorial]for combining multiple parameters[Pairwise]
- 使用进行内联测试数据
[TestCase] - 使用进行程序化生成的测试数据
[TestCaseSource] - 使用进行简单的参数组合
[Values] - 使用进行基于属性或方法的数据源
[ValueSource] - 使用生成随机数值测试数据
[Random] - 使用生成连续数值测试数据
[Range] - 使用或
[Combinatorial]来组合多个参数[Pairwise]
Assertions
断言
- Use with constraint model (preferred NUnit style)
Assert.That - Use constraints like ,
Is.EqualTo,Is.SameAsContains.Item - Use for simple value equality (classic style)
Assert.AreEqual - Use for collection comparisons
CollectionAssert - Use for string-specific assertions
StringAssert - Use or
Assert.Throws<T>to test exceptionsAssert.ThrowsAsync<T> - Use descriptive messages in assertions for clarity on failure
- 使用搭配约束模型(NUnit推荐风格)
Assert.That - 使用诸如、
Is.EqualTo、Is.SameAs等约束Contains.Item - 使用进行简单值相等性判断(经典风格)
Assert.AreEqual - 使用进行集合比较
CollectionAssert - 使用进行字符串特定断言
StringAssert - 使用或
Assert.Throws<T>测试异常Assert.ThrowsAsync<T> - 在断言中使用描述性消息,以便在失败时清晰说明问题
Mocking and Isolation
模拟与隔离
- Consider using Moq or NSubstitute alongside NUnit
- Mock dependencies to isolate units under test
- Use interfaces to facilitate mocking
- Consider using a DI container for complex test setups
- 考虑搭配使用Moq或NSubstitute与NUnit
- 模拟依赖项以隔离被测单元
- 使用接口来简化模拟
- 复杂测试设置时考虑使用DI容器
Test Organization
测试组织
- Group tests by feature or component
- Use categories with
[Category("CategoryName")] - Use to control test execution order when necessary
[Order] - Use to indicate ownership
[Author("DeveloperName")] - Use to provide additional test information
[Description] - Consider for tests that shouldn't run automatically
[Explicit] - Use to temporarily skip tests
[Ignore("Reason")]
- 按功能或组件对测试进行分组
- 使用添加分类
[Category("分类名称")] - 必要时使用控制测试执行顺序
[Order] - 使用标注所有者
[Author("开发者名称")] - 使用提供额外的测试信息
[Description] - 对于不应自动运行的测试,考虑使用
[Explicit] - 使用临时跳过测试
[Ignore("原因")]