本文へスキップ
Skillpedia
テスト40k · #7155

csharp-tunit

できること

このガイドでは、TUnitを使って効果的なコードテストを書く方法を解説します。TUnitは、プログラムが正しく動くかを確認するためのツールです。通常のテストと、さまざまなデータを使うテストの両方を扱います。

使う場面

  • ✓特定の機能を検証するテストを作るとき。
  • ✓多くの異なるシナリオでコードをテストする必要があるとき。
  • ✓xUnitなど別のテストツールから移行するとき。

スキルのリンク

https://github.com/github/awesome-copilot/blob/main/skills/csharp-tunit/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/github/awesome-copilot/blob/main/skills/csharp-tunit/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“TUnitで新しいテストプロジェクトを設定するにはどうすればよいですか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

TUnitは、コードのテストを整理するのに役立ちます。テストは、準備、実行、検証(arrange、act、assert)の流れに沿って書き、テストの実行方法やタイミングを指定する特別な属性を使います。結果の検証にも属性を使います。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

TUnit Best Practices

Your goal is to help me write effective unit tests with TUnit, covering both standard and data-driven testing approaches.

Project Setup

  • Use a separate test project with naming convention [ProjectName].Tests
  • Reference TUnit package and TUnit.Assertions for fluent assertions
  • Create test classes that match the classes being tested (e.g., CalculatorTests for Calculator)
  • Use .NET SDK test commands: dotnet test for running tests
  • TUnit requires .NET 8.0 or higher

Test Structure

  • No test class attributes required (like xUnit/NUnit)
  • Use [Test] attribute for test methods (not [Fact] like xUnit)
  • Follow the Arrange-Act-Assert (AAA) pattern
  • Name tests using the pattern MethodName_Scenario_ExpectedBehavior
  • Use lifecycle hooks: [Before(Test)] for setup and [After(Test)] for teardown
  • Use [Before(Class)] and [After(Class)] for shared context between tests in a class
  • Use [Before(Assembly)] and [After(Assembly)] for shared context across test classes
  • TUnit supports advanced lifecycle hooks like [Before(TestSession)] and [After(TestSession)]

Standard Tests

  • Keep tests focused on a single behavior
  • Avoid testing multiple behaviors in one test method
  • Use TUnit's fluent assertion syntax with await Assert.That()
  • Include only the assertions needed to verify the test case
  • Make tests independent and idempotent (can run in any order)
  • Avoid test interdependencies (use [DependsOn] attribute if needed)

Data-Driven Tests

  • Use [Arguments] attribute for inline test data (equivalent to xUnit's [InlineData])
  • Use [MethodData] for method-based test data (equivalent to xUnit's [MemberData])
  • Use [ClassData] for class-based test data
  • Create custom data sources by implementing ITestDataSource
  • Use meaningful parameter names in data-driven tests
  • Multiple [Arguments] attributes can be applied to the same test method

Assertions

  • Use await Assert.That(value).IsEqualTo(expected) for value equality
  • Use await Assert.That(value).IsSameReferenceAs(expected) for reference equality
  • Use await Assert.That(value).IsTrue() or await Assert.That(value).IsFalse() for boolean conditions
  • Use await Assert.That(collection).Contains(item) or await Assert.That(collection).DoesNotContain(item) for collections
  • Use await Assert.That(value).Matches(pattern) for regex pattern matching
  • Use await Assert.That(action).Throws<TException>() or await Assert.That(asyncAction).ThrowsAsync<TException>() to test exceptions
  • Chain assertions with .And operator: await Assert.That(value).IsNotNull().And.IsEqualTo(expected)
  • Use .Or operator for alternative conditions: await Assert.That(value).IsEqualTo(1).Or.IsEqualTo(2)
  • Use .Within(tolerance) for DateTime and numeric comparisons with tolerance
  • All assertions are asynchronous and must be awaited

Advanced Features

  • Use [Repeat(n)] to repeat tests multiple times
  • Use [Retry(n)] for automatic retry on failure
  • Use [ParallelLimit<T>] to control parallel execution limits
  • Use [Skip("reason")] to skip tests conditionally
  • Use [DependsOn(nameof(OtherTest))] to create test dependencies
  • Use [Timeout(milliseconds)] to set test timeouts
  • Create custom attributes by extending TUnit's base attributes

Test Organization

  • Group tests by feature or component
  • Use [Category("CategoryName")] for test categorization
  • Use [DisplayName("Custom Test Name")] for custom test names
  • Consider using TestContext for test diagnostics and information
  • Use conditional attributes like custom [WindowsOnly] for platform-specific tests

Performance and Parallel Execution

  • TUnit runs tests in parallel by default (unlike xUnit which requires explicit configuration)
  • Use [NotInParallel] to disable parallel execution for specific tests
  • Use [ParallelLimit<T>] with custom limit classes to control concurrency
  • Tests within the same class run sequentially by default
  • Use [Repeat(n)] with [ParallelLimit<T>] for load testing scenarios

Migration from xUnit

  • Replace [Fact] with [Test]
  • Replace [Theory] with [Test] and use [Arguments] for data
  • Replace [InlineData] with [Arguments]
  • Replace [MemberData] with [MethodData]
  • Replace Assert.Equal with await Assert.That(actual).IsEqualTo(expected)
  • Replace Assert.True with await Assert.That(condition).IsTrue()

…

よくある質問

「データ駆動」のテストとは何ですか?

異なるデータを使って何度も実行されるテストです。同じテストを何度も書く必要がありません。自分を複製して、さまざまな情報で動くテストのようなものです。

テストで何か問題が起きたかどうかを確認するには、どうすればよいですか?

`Assert.That(value).IsEqualTo(expected)`のようなコマンドを使って、実際の結果と期待した結果を比較します。一致しなければ、テストは失敗します。

テストを並列で実行できますか?

はい。TUnitは、実行を速くするためにデフォルトでテストを並列実行します。ただし、どのテストを一緒に実行するか、別々に実行するかは制御できます。

詳細

作者による元の説明

Get best practices for TUnit unit testing, including data-driven tests

repo: github/awesome-copilot

人気

GitHubのスター
40k
ランキング
#7155
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル