2015-01-29 9 views
5

xunit.net supporta "Parameterized Test Fixtures" come nunit (vedere il codice di esempio sotto). Nota Sono non alla ricerca di IUseFixture<T> o [Theory] perché questi non forniscono la stessa funzionalità Parameterized Test FixturesEsistono test di prova parametrizzati per xunit?

[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

Ottima domanda. Purtroppo ho anche la stessa domanda. –

risposta

1

Stavo cercando di passare da NUnit a xUnit, e sono stato inizialmente ingannato da questa domanda SO nel pensare che xUnit non poteva fare nulla direttamente equivalente a proiettori di prova parametrizzati.

Ma può - con [Theory]! Vedere l'aggiornamento qui sotto: xLe teorie dell'Unità non sono come le teorie NUnit, e sono in realtà molto più simili ai test parametrizzati NUnit! (Pertanto, si prega di notare, un assunto dichiarato nella domanda è falso, e penso che questa sia la migliore risposta che si può dare una volta che si toglie quel falso presupposto.)

Ecco una versione xUnit leggermente refactoring del codice che fa le stesse sei prove:

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()); 
    } 
} 

Oppure, in caso di necessità, questo codice leggermente più complesso fa le stesse sei prove guidate da esattamente gli stessi dati nella domanda iniziale:

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()); 
    } 
} 

Vedi this reference, anche se nota che PropertyDataAttribute è ormai obsoleto a favore di MemberDataAttribute.

* Aggiornamento *

Una possibile fonte di notevole confusione qui è che c'è anche un [TheoryAttribute] in NUnit, ma ha diverso importanza sotto lo stesso nome attributo nel xUnit. Ogni sotto-test in una teoria NUnit è combinato in un test che passa o fallisce (così, sì, potrebbe non essere uso realizzare semantica simile ad un dispositivo di prova NUnit parametri). Ma ogni "sotto-test" in una teoria xUnit appare nel runner come un test separato, cioè moltiplica il numero di test, molto nel modo in cui i test parametrizzati NUnit fanno!

Problemi correlati