$npx -y skills add LambdaTest/agent-skills --skill mstest-skillGenerates MSTest tests in C#. Microsoft's built-in testing framework for .NET. Use when user mentions "MSTest", "[TestMethod]", "[TestClass]", "Assert.AreEqual". Triggers on: "MSTest", "[TestMethod]", "[TestClass]", "Microsoft test framework".
| 1 | # MSTest Testing Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Basic Test |
| 6 | |
| 7 | ```csharp |
| 8 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | |
| 10 | [TestClass] |
| 11 | public class CalculatorTests |
| 12 | { |
| 13 | private Calculator _calc; |
| 14 | |
| 15 | [TestInitialize] |
| 16 | public void SetUp() => _calc = new Calculator(); |
| 17 | |
| 18 | [TestMethod] |
| 19 | public void Add_TwoNumbers_ReturnsSum() |
| 20 | { |
| 21 | Assert.AreEqual(5, _calc.Add(2, 3)); |
| 22 | } |
| 23 | |
| 24 | [TestMethod] |
| 25 | [ExpectedException(typeof(DivideByZeroException))] |
| 26 | public void Divide_ByZero_Throws() |
| 27 | { |
| 28 | _calc.Divide(10, 0); |
| 29 | } |
| 30 | } |
| 31 | ``` |
| 32 | |
| 33 | ### Data-Driven Tests |
| 34 | |
| 35 | ```csharp |
| 36 | [DataTestMethod] |
| 37 | [DataRow(2, 3, 5)] |
| 38 | [DataRow(-1, 1, 0)] |
| 39 | [DataRow(0, 0, 0)] |
| 40 | public void Add_Parameterized(int a, int b, int expected) |
| 41 | { |
| 42 | Assert.AreEqual(expected, _calc.Add(a, b)); |
| 43 | } |
| 44 | |
| 45 | [DataTestMethod] |
| 46 | [DynamicData(nameof(GetTestData), DynamicDataSourceType.Method)] |
| 47 | public void Add_DynamicData(int a, int b, int expected) |
| 48 | { |
| 49 | Assert.AreEqual(expected, _calc.Add(a, b)); |
| 50 | } |
| 51 | |
| 52 | private static IEnumerable<object[]> GetTestData() |
| 53 | { |
| 54 | yield return new object[] { 1, 2, 3 }; |
| 55 | yield return new object[] { 10, -5, 5 }; |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | ### Assertions |
| 60 | |
| 61 | ```csharp |
| 62 | Assert.AreEqual(expected, actual); |
| 63 | Assert.AreNotEqual(unexpected, actual); |
| 64 | Assert.IsTrue(condition); |
| 65 | Assert.IsFalse(condition); |
| 66 | Assert.IsNull(obj); |
| 67 | Assert.IsNotNull(obj); |
| 68 | Assert.IsInstanceOfType(obj, typeof(MyClass)); |
| 69 | Assert.ThrowsException<ArgumentException>(() => Method()); |
| 70 | CollectionAssert.Contains(list, item); |
| 71 | CollectionAssert.AreEquivalent(expected, actual); |
| 72 | StringAssert.Contains(str, "substring"); |
| 73 | StringAssert.StartsWith(str, "prefix"); |
| 74 | StringAssert.Matches(str, new Regex(@"\d+")); |
| 75 | ``` |
| 76 | |
| 77 | ### Lifecycle |
| 78 | |
| 79 | ``` |
| 80 | [AssemblyInitialize] → Once per assembly (static) |
| 81 | [ClassInitialize] → Once per class (static) |
| 82 | [TestInitialize] → Before each test |
| 83 | [TestMethod] → Test |
| 84 | [TestCleanup] → After each test |
| 85 | [ClassCleanup] → Once after class (static) |
| 86 | [AssemblyCleanup] → Once after assembly (static) |
| 87 | ``` |
| 88 | |
| 89 | ### Categories |
| 90 | |
| 91 | ```csharp |
| 92 | [TestMethod, TestCategory("Smoke")] |
| 93 | public void QuickTest() { } |
| 94 | |
| 95 | [TestMethod, Ignore("Bug #456")] |
| 96 | public void SkippedTest() { } |
| 97 | |
| 98 | [TestMethod, Timeout(5000)] |
| 99 | public void TimedTest() { } |
| 100 | ``` |
| 101 | |
| 102 | ## Setup: `dotnet add package MSTest.TestFramework MSTest.TestAdapter Microsoft.NET.Test.Sdk` |
| 103 | ## Run: `dotnet test` or `dotnet test --filter "TestCategory=Smoke"` |
| 104 | |
| 105 | ## Deep Patterns |
| 106 | |
| 107 | See `reference/playbook.md` for production-grade patterns: |
| 108 | |
| 109 | | Section | What You Get | |
| 110 | |---------|-------------| |
| 111 | | §1 Project Setup | .csproj deps, .runsettings with parallel + coverage config | |
| 112 | | §2 Test Patterns | TestMethod, DataTestMethod, DynamicData, exception testing | |
| 113 | | §3 FluentAssertions | AssertionScope, async exceptions, collection assertions | |
| 114 | | §4 Class & Assembly Initialize | Testcontainers, shared expensive setup, global config | |
| 115 | | §5 WebApplicationFactory | API integration tests with in-memory DB | |
| 116 | | §6 Bogus Test Data | Faker patterns for realistic data generation | |
| 117 | | §7 TestContext & Logging | Diagnostic output, categories, timeouts | |
| 118 | | §8 CI/CD Integration | GitHub Actions with coverage reporting and thresholds | |
| 119 | | §9 Debugging Table | 12 common problems with causes and fixes | |
| 120 | | §10 Best Practices | 14-item production checklist | |