{"kind":"Skill","metadata":{"namespace":"community","name":"csharp-mstest","version":"0.1.0"},"spec":{"description":"Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests","files":{"SKILL.md":"---\nname: csharp-mstest\ndescription: 'Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests'\n---\n\n# MSTest Best Practices (MSTest 3.x/4.x)\n\nYour goal is to help me write effective unit tests with modern MSTest, using current APIs and best practices.\n\n## Project Setup\n\n- Use a separate test project with naming convention `[ProjectName].Tests`\n- Reference MSTest 3.x+ NuGet packages (includes analyzers)\n- Consider using MSTest.Sdk for simplified project setup\n- Run tests with `dotnet test`\n\n## Test Class Structure\n\n- Use `[TestClass]` attribute for test classes\n- **Seal test classes by default** for performance and design clarity\n- Use `[TestMethod]` for test methods (prefer over `[DataTestMethod]`)\n- Follow Arrange-Act-Assert (AAA) pattern\n- Name tests using pattern `MethodName_Scenario_ExpectedBehavior`\n\n```csharp\n[TestClass]\npublic sealed class CalculatorTests\n{\n    [TestMethod]\n    public void Add_TwoPositiveNumbers_ReturnsSum()\n    {\n        // Arrange\n        var calculator = new Calculator();\n\n        // Act\n        var result = calculator.Add(2, 3);\n\n        // Assert\n        Assert.AreEqual(5, result);\n    }\n}\n```\n\n## Test Lifecycle\n\n- **Prefer constructors over `[TestInitialize]`** - enables `readonly` fields and follows standard C# patterns\n- Use `[TestCleanup]` for cleanup that must run even if test fails\n- Combine constructor with async `[TestInitialize]` when async setup is needed\n\n```csharp\n[TestClass]\npublic sealed class ServiceTests\n{\n    private readonly MyService _service;  // readonly enabled by constructor\n\n    public ServiceTests()\n    {\n        _service = new MyService();\n    }\n\n    [TestInitialize]\n    public async Task InitAsync()\n    {\n        // Use for async initialization only\n        await _service.WarmupAsync();\n    }\n\n    [TestCleanup]\n    public void Cleanup() =\u003e _service.Reset();\n}\n```\n\n### Execution Order\n\n1. **Assembly Initialization** - `[AssemblyInitialize]` (once per test assembly)\n2. **Class Initialization** - `[ClassInitialize]` (once per test class)\n3. **Test Initialization** (for every test method):\n   1. Constructor\n   2. Set `TestContext` property\n   3. `[TestInitialize]`\n4. **Test Execution** - test method runs\n5. **Test Cleanup** (for every test method):\n   1. `[TestCleanup]`\n   2. `DisposeAsync` (if implemented)\n   3. `Dispose` (if implemented)\n6. **Class Cleanup** - `[ClassCleanup]` (once per test class)\n7. **Assembly Cleanup** - `[AssemblyCleanup]` (once per test assembly)\n\n## Modern Assertion APIs\n\nMSTest provides three assertion classes: `Assert`, `StringAssert`, and `CollectionAssert`.\n\n### Assert Class - Core Assertions\n\n```csharp\n// Equality\nAssert.AreEqual(expected, actual);\nAssert.AreNotEqual(notExpected, actual);\nAssert.AreSame(expectedObject, actualObject);      // Reference equality\nAssert.AreNotSame(notExpectedObject, actualObject);\n\n// Null checks\nAssert.IsNull(value);\nAssert.IsNotNull(value);\n\n// Boolean\nAssert.IsTrue(condition);\nAssert.IsFalse(condition);\n\n// Fail/Inconclusive\nAssert.Fail(\"Test failed due to...\");\nAssert.Inconclusive(\"Test cannot be completed because...\");\n```\n\n### Exception Testing (Prefer over `[ExpectedException]`)\n\n```csharp\n// Assert.Throws - matches TException or derived types\nvar ex = Assert.Throws\u003cArgumentException\u003e(() =\u003e Method(null));\nAssert.AreEqual(\"Value cannot be null.\", ex.Message);\n\n// Assert.ThrowsExactly - matches exact type only\nvar ex = Assert.ThrowsExactly\u003cInvalidOperationException\u003e(() =\u003e Method());\n\n// Async versions\nvar ex = await Assert.ThrowsAsync\u003cHttpRequestException\u003e(async () =\u003e await client.GetAsync(url));\nvar ex = await Assert.ThrowsExactlyAsync\u003cInvalidOperationException\u003e(async () =\u003e await Method());\n```\n\n### Collection Assertions (Assert class)\n\n```csharp\nAssert.Contains(expectedItem, collection);\nAssert.DoesNotContain(unexpectedItem, collection);\nAssert.ContainsSingle(collection);  // exactly one element\nAssert.HasCount(5, collection);\nAssert.IsEmpty(collection);\nAssert.IsNotEmpty(collection);\n```\n\n### String Assertions (Assert class)\n\n```csharp\nAssert.Contains(\"expected\", actualString);\nAssert.StartsWith(\"prefix\", actualString);\nAssert.EndsWith(\"suffix\", actualString);\nAssert.DoesNotStartWith(\"prefix\", actualString);\nAssert.DoesNotEndWith(\"suffix\", actualString);\nAssert.MatchesRegex(@\"\\d{3}-\\d{4}\", phoneNumber);\nAssert.DoesNotMatchRegex(@\"\\d+\", textOnly);\n```\n\n### Comparison Assertions\n\n```csharp\nAssert.IsGreaterThan(lowerBound, actual);\nAssert.IsGreaterThanOrEqualTo(lowerBound, actual);\nAssert.IsLessThan(upperBound, actual);\nAssert.IsLessThanOrEqualTo(upperBound, actual);\nAssert.IsInRange(actual, low, high);\nAssert.IsPositive(number);\nAssert.IsNegative(number);\n```\n\n### Type Assertions\n\n```csharp\n// MSTest 3.x - uses out parameter\nAssert.IsInstanceOfType\u003cMyClass\u003e(obj, out var typed);\ntyped.DoSomething();\n\n// MSTest 4.x - returns typed result directly\nvar typed = Assert.IsInstanceOfType\u003cMyClass\u003e(obj);\ntyped.DoSomething();\n\nAssert.IsNotInstanceOfType\u003cWrongType\u003e(obj);\n```\n\n### Assert.That (MSTest 4.0+)\n\n```csharp\nAssert.That(result.Count \u003e 0);  // Auto-captures expression in failure message\n```\n\n### StringAssert Class\n\n\u003e **Note:** Prefer `Assert` class equivalents when available (e.g., `Assert.Contains(\"expected\", actual)` over `StringAssert.Contains(actual, \"expected\")`).\n\n```csharp\nStringAssert.Contains(actualString, \"expected\");\nStringAssert.StartsWith(actualString, \"prefix\");\nStringAssert.EndsWith(actualString, \"suffix\");\nStringAssert.Matches(actualString, new Regex(@\"\\d{3}-\\d{4}\"));\nStringAssert.DoesNotMatch(actualString, new Regex(@\"\\d+\"));\n```\n\n### CollectionAssert Class\n\n\u003e **Note:** Prefer `Assert` class equivalents when available (e.g., `Assert.Contains`).\n\n```csharp\n// Containment\nCollectionAssert.Contains(collection, expectedItem);\nCollectionAssert.DoesNotContain(collection, unexpectedItem);\n\n// Equality (same elements, same order)\nCollectionAssert.AreEqual(expectedCollection, actualCollection);\nCollectionAssert.AreNotEqual(unexpectedCollection, actualCollection);\n\n// Equivalence (same elements, any order)\nCollectionAssert.AreEquivalent(expectedCollection, actualCollection);\nCollectionAssert.AreNotEquivalent(unexpectedCollection, actualCollection);\n\n// Subset checks\nCollectionAssert.IsSubsetOf(subset, superset);\nCollectionAssert.IsNotSubsetOf(notSubset, collection);\n\n// Element validation\nCollectionAssert.AllItemsAreInstancesOfType(collection, typeof(MyClass));\nCollectionAssert.AllItemsAreNotNull(collection);\nCollectionAssert.AllItemsAreUnique(collection);\n```\n\n## Data-Driven Tests\n\n### DataRow\n\n```csharp\n[TestMethod]\n[DataRow(1, 2, 3)]\n[DataRow(0, 0, 0, DisplayName = \"Zeros\")]\n[DataRow(-1, 1, 0, IgnoreMessage = \"Known issue #123\")]  // MSTest 3.8+\npublic void Add_ReturnsSum(int a, int b, int expected)\n{\n    Assert.AreEqual(expected, Calculator.Add(a, b));\n}\n```\n\n### DynamicData\n\nThe data source can return any of the following types:\n\n- `IEnumerable\u003c(T1, T2, ...)\u003e` (ValueTuple) - **preferred**, provides type safety (MSTest 3.7+)\n- `IEnumerable\u003cTuple\u003cT1, T2, ...\u003e\u003e` - provides type safety\n- `IEnumerable\u003cTestDataRow\u003e` - provides type safety plus control over test metadata (display name, categories)\n- `IEnumerable\u003cobject[]\u003e` - **least preferred**, no type safety\n\n\u003e **Note:** When creating new test data methods, prefer `ValueTuple` or `TestDataRow` over `IEnumerable\u003cobject[]\u003e`. The `object[]` approach provides no compile-time type checking and can lead to runtime errors from type mismatches.\n\n```csharp\n[TestMethod]\n[DynamicData(nameof(TestData))]\npublic void DynamicTest(int a, int b, int expected)\n{\n    Assert.AreEqual(expected, Calculator.Add(a, b));\n}\n\n// ValueTuple - preferred (MSTest 3.7+)\npublic static IEnumerable\u003c(int a, int b, int expected)\u003e TestData =\u003e\n[\n    (1, 2, 3),\n    (0, 0, 0),\n];\n\n// TestDataRow - when you need custom display names or metadata\npublic static IEnumerable\u003cTestDataRow\u003c(int a, int b, int expected)\u003e\u003e TestDataWithMetadata =\u003e\n[\n    new((1, 2, 3)) { DisplayName = \"Positive numbers\" },\n    new((0, 0, 0)) { DisplayName = \"Zeros\" },\n    new((-1, 1, 0)) { DisplayName = \"Mixed signs\", IgnoreMessage = \"Known issue #123\" },\n];\n\n// IEnumerable\u003cobject[]\u003e - avoid for new code (no type safety)\npublic static IEnumerable\u003cobject[]\u003e LegacyTestData =\u003e\n[\n    [1, 2, 3],\n    [0, 0, 0],\n];\n```\n\n## TestContext\n\nThe `TestContext` class provides test run information, cancellation support, and output methods.\nSee [TestContext documentation](https://learn.microsoft.com/dotnet/core/testing/unit-testing-mstest-writing-tests-testcontext) for complete reference.\n\n### Accessing TestContext\n\n```csharp\n// Property (MSTest suppresses CS8618 - don't use nullable or = null!)\npublic TestContext TestContext { get; set; }\n\n// Constructor injection (MSTest 3.6+) - preferred for immutability\n[TestClass]\npublic sealed class MyTests\n{\n    private readonly TestContext _testContext;\n\n    public MyTests(TestContext testContext)\n    {\n        _testContext = testContext;\n    }\n}\n\n// Static methods receive it as parameter\n[ClassInitialize]\npublic static void ClassInit(TestContext context) { }\n\n// Optional for cleanup methods (MSTest 3.6+)\n[ClassCleanup]\npublic static void ClassCleanup(TestContext context) { }\n\n[AssemblyCleanup]\npublic static void AssemblyCleanup(TestContext context) { }\n```\n\n### Cancellation Token\n\nAlways use `TestContext.CancellationToken` for cooperative cancellation with `[Timeout]`:\n\n```csharp\n[TestMethod]\n[Timeout(5000)]\npublic async Task LongRunningTest()\n{\n    await _httpClient.GetAsync(url, TestContext.CancellationToken);\n}\n```\n\n### Test Run Properties\n\n```csharp\nTestContext.TestName              // Current test method name\nTestContext.TestDisplayName       // Display name (3.7+)\nTestContext.CurrentTestOutcome    // Pass/Fail/InProgress\nTestContext.TestData              // Parameterized test data (3.7+, in TestInitialize/Cleanup)\nTestContext.TestException         // Exception if test failed (3.7+, in TestCleanup)\nTestContext.DeploymentDirectory   // Directory with deployment items\n```\n\n### Output and Result Files\n\n```csharp\n// Write to test output (useful for debugging)\nTestContext.WriteLine(\"Processing item {0}\", itemId);\n\n// Attach files to test results (logs, screenshots)\nTestContext.AddResultFile(screenshotPath);\n\n// Store/retrieve data across test methods\nTestContext.Properties[\"SharedKey\"] = computedValue;\n```\n\n## Advanced Features\n\n### Retry for Flaky Tests (MSTest 3.9+)\n\n```csharp\n[TestMethod]\n[Retry(3)]\npublic void FlakyTest() { }\n```\n\n### Conditional Execution (MSTest 3.10+)\n\nSkip or run tests based on OS or CI environment:\n\n```csharp\n// OS-specific tests\n[TestMethod]\n[OSCondition(OperatingSystems.Windows)]\npublic void WindowsOnlyTest() { }\n\n[TestMethod]\n[OSCondition(OperatingSystems.Linux | OperatingSystems.MacOS)]\npublic void UnixOnlyTest() { }\n\n[TestMethod]\n[OSCondition(ConditionMode.Exclude, OperatingSystems.Windows)]\npublic void SkipOnWindowsTest() { }\n\n// CI environment tests\n[TestMethod]\n[CICondition]  // Runs only in CI (default: ConditionMode.Include)\npublic void CIOnlyTest() { }\n\n[TestMethod]\n[CICondition(ConditionMode.Exclude)]  // Skips in CI, runs locally\npublic void LocalOnlyTest() { }\n```\n\n### Parallelization\n\n```csharp\n// Assembly level\n[assembly: Parallelize(Workers = 4, Scope = ExecutionScope.MethodLevel)]\n\n// Disable for specific class\n[TestClass]\n[DoNotParallelize]\npublic sealed class SequentialTests { }\n```\n\n### Work Item Traceability (MSTest 3.8+)\n\nLink tests to work items for traceability in test reports:\n\n```csharp\n// Azure DevOps work items\n[TestMethod]\n[WorkItem(12345)]  // Links to work item #12345\npublic void Feature_Scenario_ExpectedBehavior() { }\n\n// Multiple work items\n[TestMethod]\n[WorkItem(12345)]\n[WorkItem(67890)]\npublic void Feature_CoversMultipleRequirements() { }\n\n// GitHub issues (MSTest 3.8+)\n[TestMethod]\n[GitHubWorkItem(\"https://github.com/owner/repo/issues/42\")]\npublic void BugFix_Issue42_IsResolved() { }\n```\n\nWork item associations appear in test results and can be used for:\n- Tracing test coverage to requirements\n- Linking bug fixes to regression tests\n- Generating traceability reports in CI/CD pipelines\n\n## Common Mistakes to Avoid\n\n```csharp\n// ❌ Wrong argument order\nAssert.AreEqual(actual, expected);\n// ✅ Correct\nAssert.AreEqual(expected, actual);\n\n// ❌ Using ExpectedException (obsolete)\n[ExpectedException(typeof(ArgumentException))]\n// ✅ Use Assert.Throws\nAssert.Throws\u003cArgumentException\u003e(() =\u003e Method());\n\n// ❌ Using LINQ Single() - unclear exception\nvar item = items.Single();\n// ✅ Use ContainsSingle - better failure message\nvar item = Assert.ContainsSingle(items);\n\n// ❌ Hard cast - unclear exception\nvar handler = (MyHandler)result;\n// ✅ Type assertion - shows actual type on failure\nvar handler = Assert.IsInstanceOfType\u003cMyHandler\u003e(result);\n\n// ❌ Ignoring cancellation token\nawait client.GetAsync(url, CancellationToken.None);\n// ✅ Flow test cancellation\nawait client.GetAsync(url, TestContext.CancellationToken);\n\n// ❌ Making TestContext nullable - leads to unnecessary null checks\npublic TestContext? TestContext { get; set; }\n// ❌ Using null! - MSTest already suppresses CS8618 for this property\npublic TestContext TestContext { get; set; } = null!;\n// ✅ Declare without nullable or initializer - MSTest handles the warning\npublic TestContext TestContext { get; set; }\n```\n\n## Test Organization\n\n- Group tests by feature or component\n- Use `[TestCategory(\"Category\")]` for filtering\n- Use `[TestProperty(\"Name\", \"Value\")]` for custom metadata (e.g., `[TestProperty(\"Bug\", \"12345\")]`)\n- Use `[Priority(1)]` for critical tests\n- Enable relevant MSTest analyzers (MSTEST0020 for constructor preference)\n\n## Mocking and Isolation\n\n- Use Moq or NSubstitute for mocking dependencies\n- Use interfaces to facilitate mocking\n- Mock dependencies to isolate units under test\n"},"import":{"commit_sha":"541b7819d8c3545c6df122491af4fa1eae415779","imported_at":"2026-05-18T20:05:35Z","license_text":"MIT License\n\nCopyright GitHub, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","owner":"github","repo":"github/awesome-copilot","source_url":"https://github.com/github/awesome-copilot/tree/541b7819d8c3545c6df122491af4fa1eae415779/plugins/csharp-dotnet-development/skills/csharp-mstest"}},"content_hash":[114,41,59,87,225,82,189,158,169,64,46,226,97,29,123,183,2,51,16,59,39,2,141,88,201,254,5,7,94,122,247,16],"trust_level":"unsigned","yanked":false}
