[Theory] ve [InlineData]
[Theory] ve [InlineData] ile parametreli testleri öğrenirsin.
[Theory] ve [InlineData]
[Fact] tek bir senaryoyu yoklar; ancak aynı mantığı farklı girişlerle denemek isteriz. [Theory] özniteliği, parametreli bir test metodunu işaretler; [InlineData(...)] özniteliği her veri satırını verir. Çalıştırıcı her satır için metodu ayrı bir test olarak çalıştırır.
Örnek: [Theory] [InlineData(2, 3, 5)] [InlineData(0, 0, 0)] public void TestAdd(int a, int b, int expected). Tek metot, iki test; kod tekrarsız.
Parametreli test tasarımı
Parametreli testte her [InlineData] satırı metodun imzasına uyan sabitler içerir. Metot gövdesi bu parametreleri kullanır ve tek bir Assert çağrısı yapar. Böylece yeni bir senaryo eklemek yalnızca yeni bir satır yazmaktır.
İpucu: satırlar arasında ortak kurulum gerekirse, bunu kurucuda veya TestHelper sınıfında topla. Satır verisi yalnızca giriş ve beklenen sonuç olmalı; karmaşık nesneler MemberData veya ClassData ile verilir.
Uygulama Calculator için [Theory] testi yaz
FactAttribute, TheoryAttribute, InlineDataAttribute ve statik Assert sınıflarını tanımla. InlineDataAttribute kurucusu params object[] alsın. Calculator.Add(int, int) metodu toplama yapsın. CalculatorTests.TestAdd metodu [Theory] ile işaretlenip üç [InlineData] satırı alsın: (2, 3, 5), (0, 0, 0) ve (-1, 1, 0). Gövde Assert.Equal(expected, Calculator.Add(a, b)) çağrısı yapsın. Main metodu bu testi (2, 3, 5) verisiyle çağırıp "Test passed: Theory Add(2,3)=5" yazsın.
Örnek çözümü göster
Attribute { } ve class InlineDataAttribute : Attribute { public InlineDataAttribute(params object[] data) { } } tanımla. static class Assert { public static void Equal<T>(T e, T a) { if (!EqualityComparer<T>.Default.Equals(e, a)) throw new Exception(); } } yaz. class Calculator { public static int Add(int a, int b) => a + b; } yaz. class CalculatorTests { [Theory] [InlineData(2, 3, 5)] [InlineData(0, 0, 0)] [InlineData(-1, 1, 0)] public void TestAdd(int a, int b, int expected) { Assert.Equal(expected, Calculator.Add(a, b)); } } yaz. Main'de: var t = new CalculatorTests(); t.TestAdd(2, 3, 5); Console.WriteLine("Test passed: Theory Add(2,3)=5");
Örnek çözüm:
using System;
class FactAttribute : Attribute { }
class TheoryAttribute : Attribute { }
class InlineDataAttribute : Attribute
{
public InlineDataAttribute(params object[] data) { }
}
static class Assert
{
public static void Equal<T>(T expected, T actual)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw new Exception($"Beklenen {expected}, gerçek {actual}");
}
}
class Calculator
{
public static int Add(int a, int b) => a + b;
}
class CalculatorTests
{
[Theory]
[InlineData(2, 3, 5)]
[InlineData(0, 0, 0)]
[InlineData(-1, 1, 0)]
public void TestAdd(int a, int b, int expected)
{
Assert.Equal(expected, Calculator.Add(a, b));
}
}
class Program
{
static void Main()
{
var tests = new CalculatorTests();
tests.TestAdd(2, 3, 5);
Console.WriteLine("Test passed: Theory Add(2,3)=5");
}
}
Kendini test et
-
[Theory] ile [Fact] arasındaki fark nedir?
- [Theory] yalnızca asenkron metotlarda kullanılır
- [Theory] daha yavaş çalışır
- [Theory] hiçbir zaman [InlineData] gerektirmez
- [Theory] parametreli testi birden çok veriyle çalıştırır
Cevabı göster
[Theory] parametreli testi birden çok veriyle çalıştırır — Doğru. [Theory] + [InlineData] tek metodu birden çok veriyle çalıştırır.
-
InlineData satırını tamamla
[InlineData(____, 3, 5)] public void TestAdd(int a, int b, int expected) { Assert.Equal(expected, Calculator.Add(a, b)); }Cevabı göster
[InlineData(2, 3, 5)] public void TestAdd(int a, int b, int expected) { Assert.Equal(expected, Calculator.Add(a, b)); } -
Öznitelikleri rolüyle eşleştir
- [Fact]
- Tek senaryolu test
- [Theory]
- Parametreli test
- [InlineData]
- Bir veri satırı
- Assert
- Doğrulama