Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
MSTest testleri düzenlemek, meta veriler eklemek ve testleri iş izleme sistemlerine bağlamak için öznitelikler sağlar. Bu öznitelikler büyük test paketlerinde testleri etkili bir şekilde filtrelemenize, sıralamanıza ve yönetmenize yardımcı olur.
Genel Bakış
Test yöntemleri için Visual Studio Özellikleri penceresinde meta veri öznitelikleri görüntülenir. Bunlar size yardımcı olur:
- Testleri düzenleme: Testleri kategoriye, önceliğe veya sahipe göre gruplandırma.
- Filtre testi çalıştırmaları: Meta verilere göre testlerin belirli alt kümelerini çalıştırın.
- Test kapsamını izleme: Testleri iş öğelerine ve gereksinimlere bağlayın.
- Rapor oluşturma: Test raporlarına ve panolarına meta veriler ekleyin.
Kategorilere ayırmayı test et
TestCategoryAttribute
TestCategoryAttribute testleri filtreleme ve düzenleme amacıyla kategorilere ayırır. Bu özniteliği yöntem, sınıf veya derleme düzeyinde uygulayabilirsiniz ve kategoriler birden çok düzeyde uygulandığında birleştirilir.
Yöntem düzeyi kategorileri
Ayrıntılı denetim için doğrudan test yöntemlerine kategorileri uygulayın:
[TestClass]
public class OrderTests
{
[TestMethod]
[TestCategory("Integration")]
public void CreateOrder_SavesOrderToDatabase()
{
// Integration test
}
[TestMethod]
[TestCategory("Unit")]
public void CalculateTotal_ReturnsSumOfItems()
{
// Unit test
}
[TestMethod]
[TestCategory("Integration")]
[TestCategory("Slow")]
public void ProcessLargeOrder_CompletesSuccessfully()
{
// Multiple categories allowed
}
}
Sınıf düzeyi kategoriler
Bu kategoriyi sınıfın içindeki tüm test yöntemlerine atamak için bir test sınıfına kategori uygulayın:
[TestClass]
[TestCategory("Payments")]
public class PaymentServiceTests
{
[TestMethod]
public void ProcessPayment_ValidCard_Succeeds()
{
// Inherits "Payments" category from class
}
[TestMethod]
[TestCategory("Slow")]
public void ProcessBatchPayments_LargeVolume_CompletesSuccessfully()
{
// Has both "Payments" (from class) and "Slow" (from method) categories
}
}
Derleme düzeyi kategorileri
Tüm test derlemesindeki tüm testleri kategorilere ayırmak için derleme düzeyinde bir kategori uygulayın. Bu yaklaşım, test türlerini projeler arasında ayırt etmek için kullanışlıdır:
// In AssemblyInfo.cs or any file in your test project
using Microsoft.VisualStudio.TestTools.UnitTesting;
[assembly: TestCategory("E2E")]
Test projelerinizi test türüne göre düzenlemek için derleme düzeyi kategorilerini kullanın:
| Proje | Montaj kategorisi | Amaç |
|---|---|---|
MyApp.UnitTests |
Unit |
Hızlı, yalıtılmış birim testleri |
MyApp.IntegrationTests |
Integration |
Dış bağımlılıklara sahip testler |
MyApp.E2ETests |
E2E |
Uçtan uca senaryo testleri |
Testleri kategoriye göre filtreleme
Komutunu kullanarak testleri kategoriye göre dotnet test çalıştırın:
# Run only integration tests
dotnet test --filter TestCategory=Integration
# Run tests in multiple categories
dotnet test --filter "TestCategory=Integration|TestCategory=Unit"
# Exclude slow tests
dotnet test --filter TestCategory!=Slow
Visual Studio Test Gezgini'nde, ön ekli Trait: arama kutusunu kullanın:
-
Trait:"TestCategory=Integration"- tümleştirme testlerini gösterir -
-Trait:"TestCategory=Slow"- Yavaş testleri dışlar
TestPropertyAttribute
, TestPropertyAttribute testlere özel anahtar-değer meta verileri ekler. Yerleşik öznitelikler gereksinimlerinizi karşılamadığında bu özniteliği kullanın.
[TestClass]
public class CustomMetadataTests
{
[TestMethod]
[TestProperty("Feature", "Authentication")]
[TestProperty("Sprint", "23")]
[TestProperty("RiskLevel", "High")]
public void Login_WithValidCredentials_Succeeds()
{
// Test with custom properties
}
[TestMethod]
[TestProperty("Feature", "Authorization")]
[TestProperty("RequirementId", "REQ-AUTH-001")]
public void AccessAdminPage_RequiresAdminRole()
{
// Link to requirements
}
}
Özellikler, Visual Studio Özellikleri penceresinde Belirli testin altında görünür.
Özel özelliklere göre filtreleme
# Filter by custom property
dotnet test --filter "Feature=Authentication"
Sahipliği ve önceliği test edin
OwnerAttribute
, OwnerAttribute testlerden kimin sorumlu olduğunu tanımlar.
[TestClass]
public class PaymentTests
{
[TestMethod]
[Owner("jsmith")]
public void ProcessPayment_ChargesCorrectAmount()
{
// John Smith owns this test
}
[TestMethod]
[Owner("team-payments")]
public void RefundPayment_CreditsCustomerAccount()
{
// Team responsibility
}
}
Testleri sahibine göre filtreleyin:
dotnet test --filter Owner=jsmith
PriorityAttribute
PriorityAttribute göreli testin önemini gösterir. Düşük değerler daha yüksek önceliğe işaret eder.
[TestClass]
public class CriticalPathTests
{
[TestMethod]
[Priority(0)]
public void Login_IsAlwaysAvailable()
{
// Highest priority - core functionality
}
[TestMethod]
[Priority(1)]
public void CreateAccount_WorksCorrectly()
{
// High priority
}
[TestMethod]
[Priority(2)]
public void CustomizeProfile_SavesPreferences()
{
// Medium priority
}
}
Testleri önceliğe göre filtreleyin:
# Run only highest priority tests
dotnet test --filter Priority=0
# Run high priority or higher
dotnet test --filter "Priority=0|Priority=1"
DescriptionAttribute
, DescriptionAttribute testin neyi doğrulayanı insan tarafından okunabilir bir açıklama sağlar.
Uyarı
Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute'yi, System.ComponentModel.DescriptionAttribute'ye tercih edin.
MSTEST0031 çözümleyicisi yanlış kullanım algılar.
[TestClass]
public class DocumentedTests
{
[TestMethod]
[Description("Verifies that orders over $100 receive a 10% discount")]
public void ApplyDiscount_LargeOrder_Gets10PercentOff()
{
// Test implementation
}
[TestMethod]
[Description("Ensures email validation rejects malformed addresses")]
public void ValidateEmail_InvalidFormat_ReturnsFalse()
{
// Test implementation
}
}
İş öğesi izleme
WorkItemAttribute
WorkItemAttribute bağlantıları, izleme sisteminizdeki iş öğeleriyle (Azure DevOps gibi) testleri ilişkilendirir.
[TestClass]
public class BugFixTests
{
[TestMethod]
[WorkItem(12345)]
[Description("Regression test for bug #12345")]
public void DatePicker_LeapYear_HandlesFebruary29()
{
// Test that verifies the bug fix
}
[TestMethod]
[WorkItem(67890)]
[WorkItem(67891)] // Can link multiple work items
public void Export_LargeDataset_CompletesWithinTimeout()
{
// Test related to multiple work items
}
}
GitHubWorkItemAttribute
Testler GitHubWorkItemAttribute GitHub sorunlarına bağlanır.
Uyarı
MSTest GitHubWorkItemAttribute 3.8'de tanıtıldı.
[TestClass]
public class GitHubLinkedTests
{
[TestMethod]
[GitHubWorkItem("https://github.com/myorg/myrepo/issues/42")]
public void FeatureX_WorksAsExpected()
{
// Test linked to GitHub issue #42
}
[TestMethod]
[GitHubWorkItem("https://github.com/myorg/myrepo/issues/100")]
[Ignore("Waiting for upstream fix")]
public void DependentFeature_RequiresUpdate()
{
// Ignored test linked to tracking issue
}
}
İş öğesi öznitelikleri, özellikle Ignore ile birleştirildiğinde değerlidir.
[TestMethod]
[Ignore("Known issue, tracked in work item")]
[WorkItem(99999)]
public void KnownIssue_AwaitingFix()
{
// Provides traceability for why the test is ignored
}
Öznitelikleri birleştirme
Kapsamlı test organizasyonu için birden çok meta veri özniteliğini birleştirin:
[TestClass]
public class FullyDocumentedTests
{
[TestMethod]
[TestCategory("Integration")]
[TestCategory("API")]
[Owner("payment-team")]
[Priority(1)]
[Description("Verifies that the payment API returns correct error codes for invalid requests")]
[WorkItem(54321)]
[TestProperty("Sprint", "24")]
[TestProperty("Feature", "ErrorHandling")]
public void PaymentAPI_InvalidRequest_ReturnsAppropriateErrorCode()
{
// Well-documented test with full traceability
}
}
En iyi yöntemler
Tutarlı kategoriler kullanın: Projenizde test kategorileri için adlandırma kuralları oluşturun.
Öncelikleri stratejik olarak ayarlayın: Uygulanabilir bir derleme için geçmesi gereken kritik yol testleri için ayırın
Priority(0).İş öğelerine bağlantı: İzlenebilirlik için testleri, özellikle hata regresyon testlerini her zaman ilgili iş öğelerine bağlayın.
Belge testi amacı: Yöntem adının amacı tam olarak açıklamadığı karmaşık testler için kullanın
Description.Meta verileri güncel tutun: Test kapsamı veya sahipliği değiştiğinde meta verileri güncelleştirin.
Filtreleme için kategorileri kullanın: CI/CD işlem hattı gereksinimlerinizi desteklemek için kategoriler tasarlayın (örneğin, "Duman", "Gecelik", "Tümleştirme").
Filtreleme testleri
Komut satırı filtreleme
# Filter by category
dotnet test --filter TestCategory=Unit
# Filter by owner
dotnet test --filter Owner=jsmith
# Filter by priority
dotnet test --filter Priority=0
# Combine filters (AND)
dotnet test --filter "TestCategory=Integration&Priority=0"
# Combine filters (OR)
dotnet test --filter "TestCategory=Smoke|TestCategory=Critical"
# Exclude by filter
dotnet test --filter TestCategory!=Slow
# Filter by custom property
dotnet test --filter "Feature=Payments"
Visual Studio Test Keşfetçisi
Test Gezgini'nde arama kutusunu kullanın:
-
Trait:"TestCategory=Integration"- kategoriye göre filtrele -
Trait:"Owner=jsmith"- sahibine göre filtrele -
Trait:"Priority=0"- önceliğe göre filtrele
Test filtreleme hakkında daha fazla bilgi için bkz. Seçmeli birim testlerini çalıştırma.