2015-01-29 26 views
5

xunit.net, nunit gibi "Parametreli Test Armatürlerini" destekliyor mu (aşağıdaki örnek koda bakın). https://github.com/vytautas-mackonis/xUnit.Paradigms: Bu sadece bu projeyi bulundu Parameterized Test FixturesXunit için Parametreli Test Armatürleri Var mı?

[TestFixture("hello", "hello", "goodbye")] 
[TestFixture("zip", "zip")] 
[TestFixture(42, 42, 99)] 
public class ParameterizedTestFixture 
{ 
    private string eq1; 
    private string eq2; 
    private string neq; 

    public ParameterizedTestFixture(string eq1, string eq2, string neq) 
    { 
     this.eq1 = eq1; 
     this.eq2 = eq2; 
     this.neq = neq; 
    } 

    public ParameterizedTestFixture(string eq1, string eq2) 
     : this(eq1, eq2, null) { } 

    public ParameterizedTestFixture(int eq1, int eq2, int neq) 
    { 
     this.eq1 = eq1.ToString(); 
     this.eq2 = eq2.ToString(); 
     this.neq = neq.ToString(); 
    } 

    [Test] 
    public void TestEquality() 
    { 
     Assert.AreEqual(eq1, eq2); 
     if (eq1 != null && eq2 != null) 
      Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode()); 
    } 

    [Test] 
    public void TestInequality() 
    { 
     Assert.AreNotEqual(eq1, neq); 
     if (eq1 != null && neq != null) 
      Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode()); 
    } 
} 
+0

Harika bir soru. Ne yazık ki aynı sorularım var. –

cevap

0

aynı işlevselliği sağlamak değil çünkü değilIUseFixture<T> veya [Theory] arayan değilim unutmayın. Hala xUnit'ın 1.9.2 sürümü üzerindedir.

1

NUnit'ten xUnit'e geçmeyi düşünüyordum ve başlangıçta bu SO sorusu tarafından xUnit'in parametreli test fikstürleriyle doğrudan eşdeğer bir şey yapamayacağını düşünerek yanlış yönlendirildi.

Ancak bunu yapabilir - [Theory] kullanarak! Aşağıdaki güncelleştirmeye bakın: xUnit teorileri NUnit teorileri gibi değildir ve aslında NUnit parametreli testlere çok benzerler! (Bu nedenle, soruda belirtilen bir varsayımın yanlış olduğunu ve bu yanlış varsayımı kaldırdıktan sonra verilebilecek en iyi yanıt olduğunu düşünüyorum.)

İşte bir rengin geri yüklenen xUnit sürümü aynı altı testleri yapar kodu:

: durumunda,

public class ParameterizedTestFixture 
{ 
    public static IEnumerable<object[]> TestCases = new[] { 
     new object[] { "hello", "hello", "goodbye" }, 
     new object[] { "zip", "zip", null }, 
     new object[] { "42", "42", "99" } 
    }; 

    [Theory] 
    [MemberData(nameof(TestCases))] 
    public void TestEquality(string eq1, string eq2, string neq) 
    { 
     Assert.Equal(eq1, eq2); 
     if(eq1 != null && eq2 != null) 
      Assert.Equal(eq1.GetHashCode(), eq2.GetHashCode()); 
    } 

    [Theory] 
    [MemberData(nameof(TestCases))] 
    public void TestInequality(string eq1, string eq2, string neq) 
    { 
     Assert.NotEqual(eq1, neq); 
     if(eq1 != null && neq != null) 
      Assert.NotEqual(eq1.GetHashCode(), neq.GetHashCode()); 
    } 
} 

Yoksa bu biraz daha karmaşık kod orijinal söz konusu olarak tam aynı verilere tarafından yönlendirilen aynı altı testleri yapar, ihtiyaç

public class ParameterizedTestFixture 
{ 
    public static IEnumerable<object[]> TestCases = new[] { 
     new object[] { "hello", "hello", "goodbye" }, 
     new object[] { "zip", "zip", null }, 
     new object[] { 42, 42, 99 } 
    }; 

    private string eq1; 
    private string eq2; 
    private string neq; 

    public void Init(object _eq1, object _eq2, object _neq) 
    { 
     this.eq1 = (_eq1 == null ? null : _eq1.ToString()); 
     this.eq2 = (_eq2 == null ? null : _eq2.ToString()); 
     this.neq = (_neq == null ? null : _neq.ToString()); 
    } 

    [Theory] 
    [MemberData(nameof(TestCases))] 
    public void TestEquality(object _eq1, object _eq2, object _neq) 
    { 
     Init(_eq1, _eq2, _neq); 
     Assert.Equal(eq1, eq2); 
     if(eq1 != null && eq2 != null) 
      Assert.Equal(eq1.GetHashCode(), eq2.GetHashCode()); 
    } 

    [Theory] 
    [MemberData(nameof(TestCases))] 
    public void TestInequality(object _eq1, object _eq2, object _neq) 
    { 
     Init(_eq1, _eq2, _neq); 
     Assert.NotEqual(eq1, neq); 
     if(eq1 != null && neq != null) 
      Assert.NotEqual(eq1.GetHashCode(), neq.GetHashCode()); 
    } 
} 

Bkz. PropertyDataAttribute artık MemberDataAttribute lehine artık geçerli değil. Ancak, this reference.

* Güncelleme *

önemli burada karışıklık var da NUnit bir [TheoryAttribute], ancak xUnit aynı adlı özniteliğinden farklı öneme sahip olmasıdır olası bir kaynağı. Bir NUnit teorisindeki her bir alt test, geçen veya başarısız olan bir testte birleştirilir (bu nedenle, ,, bir NUnit parametreli test fikstürüne benzer semantikleri elde etmek için kullanılamaz). Fakat xUnit teorisindeki her 'alt-test' koşucuda ayrı bir test olarak görünür, yani NUnit parametreli testlerin yaptığı gibi test sayısını arttırır!

İlgili konular