मुख्य सामग्री पर जाएँ
Skillpedia
टेस्टिंग40k · #7153

csharp-mstest

यह क्या करती है

यह गाइड बताती है कि MSTest टूल का इस्तेमाल करके अच्छे कोड टेस्ट कैसे लिखे जाते हैं। इसमें टेस्ट कोड को व्यवस्थित करने, नतीजों की जाँच करने और errors संभालने के सबसे अच्छे तरीके दिखाए गए हैं।

कब इस्तेमाल करें

  • ✓यह जाँचने के लिए टेस्ट बनाते समय कि आपका कोड सही काम करता है या नहीं।
  • ✓जब आपको यह पक्का करना हो कि प्रोग्राम का कोई खास हिस्सा अपेक्षा के अनुसार व्यवहार करता है।
  • ✓MSTest टूल से unit testing सीखते समय।

Skill का लिंक

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

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/github/awesome-copilot/blob/main/skills/csharp-mstest/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“मैं कैसे टेस्ट करूँ कि addition function दो positive numbers के लिए सही value लौटाता है?”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

यह गाइड टेस्ट कोड को classes और methods में व्यवस्थित करती है, और नतीजों को लिखने और जाँचने का एक साफ़ pattern फॉलो करती है। यह values की तुलना करने, null की जाँच करने, या यह देखने के लिए कि exception फेंका गया या नहीं, खास commands इस्तेमाल करती है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

MSTest Best Practices (MSTest 3.x/4.x)

Your goal is to help me write effective unit tests with modern MSTest, using current APIs and best practices.

Project Setup

  • Use a separate test project with naming convention [ProjectName].Tests
  • Reference MSTest 3.x+ NuGet packages (includes analyzers)
  • Consider using MSTest.Sdk for simplified project setup
  • Run tests with dotnet test

Test Class Structure

  • Use [TestClass] attribute for test classes
  • Seal test classes by default for performance and design clarity
  • Use [TestMethod] for test methods (prefer over [DataTestMethod])
  • Follow Arrange-Act-Assert (AAA) pattern
  • Name tests using pattern MethodName_Scenario_ExpectedBehavior
[TestClass]
public sealed class CalculatorTests
{
    [TestMethod]
    public void Add_TwoPositiveNumbers_ReturnsSum()
    {
        // Arrange
        var calculator = new Calculator();

        // Act
        var result = calculator.Add(2, 3);

        // Assert
        Assert.AreEqual(5, result);
    }
}

Test Lifecycle

  • Prefer constructors over [TestInitialize] - enables readonly fields and follows standard C# patterns
  • Use [TestCleanup] for cleanup that must run even if test fails
  • Combine constructor with async [TestInitialize] when async setup is needed
[TestClass]
public sealed class ServiceTests
{
    private readonly MyService _service;  // readonly enabled by constructor

    public ServiceTests()
    {
        _service = new MyService();
    }

    [TestInitialize]
    public async Task InitAsync()
    {
        // Use for async initialization only
        await _service.WarmupAsync();
    }

    [TestCleanup]
    public void Cleanup() => _service.Reset();
}

Execution Order

  1. Assembly Initialization - [AssemblyInitialize] (once per test assembly)
  2. Class Initialization - [ClassInitialize] (once per test class)
  3. Test Initialization (for every test method):
    1. Constructor
    2. Set TestContext property
    3. [TestInitialize]
  4. Test Execution - test method runs
  5. Test Cleanup (for every test method):
    1. [TestCleanup]
    2. DisposeAsync (if implemented)
    3. Dispose (if implemented)
  6. Class Cleanup - [ClassCleanup] (once per test class)
  7. Assembly Cleanup - [AssemblyCleanup] (once per test assembly)

Modern Assertion APIs

MSTest provides three assertion classes: Assert, StringAssert, and CollectionAssert.

Assert Class - Core Assertions

// Equality
Assert.AreEqual(expected, actual);
Assert.AreNotEqual(notExpected, actual);
Assert.AreSame(expectedObject, actualObject);      // Reference equality
Assert.AreNotSame(notExpectedObject, actualObject);

// Null checks
Assert.IsNull(value);
Assert.IsNotNull(value);

// Boolean
Assert.IsTrue(condition);
Assert.IsFalse(condition);

// Fail/Inconclusive
Assert.Fail("Test failed due to...");
Assert.Inconclusive("Test cannot be completed because...");

Exception Testing (Prefer over [ExpectedException])

// Assert.Throws - matches TException or derived types
var ex = Assert.Throws<ArgumentException>(() => Method(null));
Assert.AreEqual("Value cannot be null.", ex.Message);

// Assert.ThrowsExactly - matches exact type only
var ex = Assert.ThrowsExactly<InvalidOperationException>(() => Method());

// Async versions
var ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await client.GetAsync(url));
var ex = await Assert.ThrowsExactlyAsync<InvalidOperationException>(async () => await Method());

Collection Assertions (Assert class)

Assert.Contains(expectedItem, collection);
Assert.DoesNotContain(unexpectedItem, collection);
Assert.ContainsSingle(collection);  // exactly one element
Assert.HasCount(5, collection);
Assert.IsEmpty(collection);
Assert.IsNotEmpty(collection);

String Assertions (Assert class)

Assert.Contains("expected", actualString);
Assert.StartsWith("prefix", actualString);
Assert.EndsWith("suffix", actualString);
Assert.DoesNotStartWith("prefix", actualString);
Assert.DoesNotEndWith("suffix", actualString);
Assert.MatchesRegex(@"\d{3}-\d{4}", phoneNumber);
Assert.DoesNotMatchRegex(@"\d+", textOnly);

Comparison Assertions

Assert.IsGreaterThan(lowerBound, actual);
Assert.IsGreaterThanOrEqualTo(lowerBound, actual);
Assert.IsLessThan(upperBound, actual);
Assert.IsLessThanOrEqualTo(upperBound, actual);

…
अक्सर पूछे जाने वाले सवाल

unit test क्या है?

यह एक छोटा कोड है जो जाँचता है कि आपके कोड का कोई दूसरा हिस्सा (आमतौर पर कोई function या method) सही काम कर रहा है या नहीं।

अलग test project क्यों बनाएँ?

इससे आपका मुख्य कोड साफ़ और व्यवस्थित रहता है, क्योंकि प्रोग्राम चलाने वाले कोड को उस कोड से अलग रखा जाता है जो यह जाँचता है कि प्रोग्राम सही है या नहीं।

Arrange-Act-Assert pattern क्या है?

टेस्ट लिखने का एक आम तरीका: 1. Arrange: जो कुछ चाहिए उसे तैयार करें। 2. Act: जिस कोड का टेस्ट करना है उसे चलाएँ। 3. Assert: जाँचें कि नतीजा वही है जो आप उम्मीद कर रहे थे।

जानकारी

लेखक का मूल विवरण

Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests

repo: github/awesome-copilot

लोकप्रियता

GitHub स्टार
40k
रैंकिंग में स्थान
#7153
लाइसेंस
MIT
अपडेट की गई
15/10/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

acquire-codebase-knowledge

40k

आपके एजेंट को किसी मौजूदा code प्रोजेक्ट को समझने और उसका documentation बनाने में मदद करता है। यह नए लोगों को प्रोजेक्ट से जोड़ने या architecture का सार जानने में उपयोगी है।

लेखन और डॉक्यूमेंटडॉक्यूमेंटेशन

acreadiness-assess

40k

आपके एजेंट को किसी प्रोजेक्ट की AI के लिए तैयारी जाँचने और रिपोर्ट बनाने देता है। यह समझने में उपयोगी है कि प्रोजेक्ट AI tools इस्तेमाल करने के लिए कितना तैयार है।

लेखन और डॉक्यूमेंटडॉक्यूमेंटेशन

acreadiness-generate-instructions

40k

आपके एजेंट को assessment के आधार पर AI tools के लिए निर्देश बनाने में मदद करता है। यह बताता है कि AI कोड के साथ बेहतर कैसे काम करे और पहचानी गई कमियाँ कैसे दूर हों।

लेखन और डॉक्यूमेंटडॉक्यूमेंटेशन

acreadiness-policy

40k

आपके एजेंट को AI प्रोजेक्ट assessment के लिए नीतियाँ तय करने या लागू करने में मदद करता है। यह प्रोजेक्ट्स के मूल्यांकन को मानक बनाने और compliance सुनिश्चित करने में उपयोगी है।

AI और एजेंटएजेंट

मिलती-जुलती skills