Chuyển đến nội dung
Skillpedia
Kiểm thử40k · #7156

csharp-xunit

Skill này làm gì

Công cụ này giúp bạn viết kiểm thử code hiệu quả bằng XUnit, một thư viện phổ biến để kiểm thử chương trình C#. Nó dạy thực hành tốt nhất cho cả kiểm thử đơn giản lẫn kiểm thử với dữ liệu đa dạng.

Khi nào nên dùng

  • ✓Khi bạn muốn bảo đảm code C# hoạt động như mong đợi.
  • ✓Khi tạo kiểm thử cần nhiều bộ dữ liệu để chạy.
  • ✓Để học cách tổ chức và viết kiểm thử rõ ràng, dễ tái sử dụng.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/csharp-xunit/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để viết kiểm thử cho hàm “Add” bằng XUnit, kiểm tra xem 2 + 2 có bằng 4 không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ đưa ra ví dụ và quy tắc để thiết lập dự án kiểm thử, cấu trúc các kiểm thử (tách phần chuẩn bị, hành động và kiểm tra) và dùng các loại kiểm thử khác nhau, như kiểm thử nhận dữ liệu đa dạng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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âu hỏi thường gặp

Kiểm thử đơn vị là gì?

Đó là một kiểm thử nhỏ, kiểm tra xem một phần cụ thể của code (như một hàm) có hoạt động đúng khi đứng riêng không.

Kiểm thử “data-driven” là gì?

Đó là các kiểm thử chạy cùng một logic nhiều lần, mỗi lần với một bộ dữ liệu khác nhau, để bao phủ nhiều tình huống hơn.

Tôi có cần biết nhiều về C# để dùng công cụ này không?

Nên có kiến thức cơ bản về C# và cách code của bạn hoạt động, nhưng công cụ được thiết kế để hướng dẫn bạn các thực hành tốt nhất khi kiểm thử.

Chi tiết

Mô tả gốc của tác giả

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

repo: github/awesome-copilot

Độ phổ biến

Sao GitHub
40k
Vị trí xếp hạng
#7156
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự