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

csharp-xunit

できること

このツールは、C#プログラムをテストするための人気のあるライブラリであるxUnitを使って、効率的なコードテストを書くのに役立ちます。シンプルなテストと、さまざまなデータを使うテストの両方について、ベストプラクティスを教えます。

使う場面

  • ✓C#コードが期待どおりに動くことを確かめたいとき。
  • ✓動作させるために複数のデータセットを必要とするテストを作るとき。
  • ✓テストを分かりやすく、再利用しやすい形で書けるようになりたいとき。

スキルのリンク

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

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

使い方

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

貼り付け用メッセージ

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

AIへの頼み方の例

“xUnitを使って、2 + 2 = 4 になるかを確認する「Add」関数のテストを書くにはどうすればよいですか?”

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

仕組み

このツールは、テストプロジェクトの設定方法、テストの構成(準備、実行、検証の分離)、さまざまなデータを受け取るテストなど、異なる種類のテストの使い方について、例とルールを示します。

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

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

XUnit Best Practices

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

Project Setup

  • Use a separate test project with naming convention [ProjectName].Tests
  • Reference Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio packages
  • Create test classes that match the classes being tested (e.g., CalculatorTests for Calculator)
  • Use .NET SDK test commands: dotnet test for running tests

Test Structure

  • No test class attributes required (unlike MSTest/NUnit)
  • Use fact-based tests with [Fact] attribute for simple tests
  • Follow the Arrange-Act-Assert (AAA) pattern
  • Name tests using the pattern MethodName_Scenario_ExpectedBehavior
  • Use constructor for setup and IDisposable.Dispose() for teardown
  • Use IClassFixture<T> for shared context between tests in a class
  • Use ICollectionFixture<T> for shared context between multiple test classes

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 [Theory] combined with data source attributes
  • Use [InlineData] for inline test data
  • Use [MemberData] for method-based test data
  • Use [ClassData] for class-based test data
  • Create custom data attributes by implementing DataAttribute
  • Use meaningful parameter names in data-driven tests

Assertions

  • Use Assert.Equal for value equality
  • Use Assert.Same for reference equality
  • Use Assert.True/Assert.False for boolean conditions
  • Use Assert.Contains/Assert.DoesNotContain for collections
  • Use Assert.Matches/Assert.DoesNotMatch for regex pattern matching
  • Use Assert.Throws<T> or await Assert.ThrowsAsync<T> to test exceptions
  • Use fluent assertions library for more readable assertions

Mocking and Isolation

  • Consider using Moq or NSubstitute alongside XUnit
  • Mock dependencies to isolate units under test
  • Use interfaces to facilitate mocking
  • Consider using a DI container for complex test setups

Test Organization

  • Group tests by feature or component
  • Use [Trait("Category", "CategoryName")] for categorization
  • Use collection fixtures to group tests with shared dependencies
  • Consider output helpers (ITestOutputHelper) for test diagnostics
  • Skip tests conditionally with Skip = "reason" in fact/theory attributes
よくある質問

単体テストとは何ですか?

関数などの特定の部分が、ほかから切り離された状態で正しく動くかを確認する、小さなテストのことです。

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

同じロジックを、毎回異なるデータセットで何度も実行し、より多くのケースをカバーするテストです。

このツールを使うのにC#の知識は多く必要ですか?

C#の基本とコードの動きが分かっていると役立ちますが、このツールはテストのベストプラクティスに沿って案内するよう設計されています。

詳細

作者による元の説明

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

repo: github/awesome-copilot

人気

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

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

0

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

コメント

  • …

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

似ているスキル