2014-11-14 17 views
5

Sto provando ad usare TestCaseSource in NUnit per eseguire test multipli con uno dei parametri essendo un arrayTestCase nome visualizzato in NUnit Quando si utilizzano matrici o Generics

private static readonly object[] ReturnChopCases = 
    { 
     new TestCaseData(3, new List<int> {}).Returns(-1), 
     new TestCaseData(3, new List<int> {1}).Returns(1), 
     new TestCaseData(1, new List<int> {1,2}).Returns(1), 
    }; 

    [TestCaseSource("ReturnChopCases")] 
    public int test_chop(int SearchNumber, int[] SearchArray) 
    { 
     return Chopper.Chop(3, SearchArray); 
    } 

Il problema è il nome visualizzato nella test runner (Sto usando il NUnit Test Adapter) è abbastanza inutile, tutti mostrano come test_chop(0,System.Int32[]) o se si utilizza un List quindi test_chop(0,System.Collections.Generic.List`1[System.Int32]).

Come mantenere un test abbastanza leggibile e fornire un nome di prova utile al test nel corridore di prova? Ho provato alcune cose ma ho ancora il nome di cui sopra.

+2

È possibile nominare i test in modo esplicito se si virano su '.SetName (" il tuo nome ")'. Forse stai cercando qualcosa di più automatico. –

+0

Sì, ero davvero sorpreso che non facesse qualcosa di meglio con gli array in particolare. '.SetName (" ")' sembra l'opzione migliore o potrei scrivere un metodo per passare un nome per questi test. –

risposta

2

utilizzare la funzione SetName dare un nome al test

new TestCaseData(3, new List<int> {}).Returns(-1).SetName("test_chop_List<int>"), 
+0

Anche se mi piacerebbe che ci fosse una soluzione migliore questo sembra, grazie :) –

1

Questa è la soluzione sono andato con, in primo luogo ho creato una classe per mia abitudine test case

public class CustomTestCase 
{ 
    public CustomTestCase(int SearchNumber, List<int> List, int Result) 
    { 
     searchNumber = SearchNumber; 
     list = List; 
     result = Result; 
    } 
    private int searchNumber; 
    private List<int> list; 
    private int result; 

    public string TestName() 
    { 
     return string.Format("TestChop({0},{{{1}}})", searchNumber, String.Join(",", list)); 
    } 

    public TestCaseData SimpleTest() 
    { 
     return new TestCaseData(searchNumber, list).Returns(result).SetName(this.TestName()); 
    } 
} 

E poi ho usato che per costruire l'TestCaseSource

private static IEnumerable ReturnChopCases() 
{ 
    List<int> emptyList = new List<int> { }; 
    yield return new CustomTestCase(3,emptyList,-1).SimpleTest(); 

    List<int> singleItemList = new List<int> { 1 }; 
    yield return new CustomTestCase(3, singleItemList, 1).SimpleTest(); 
    yield return new CustomTestCase(3, singleItemList, 1).SimpleTest(); 
} 

il test è lo stesso.

Continuo a pensare che NUnit dovrebbe generare più nomi utili ma ho trovato questo il modo più semplice per gestire il mio problema e se voglio gestire le eccezioni, potrei semplicemente creare un altro metodo per gestirli.

NB: non dimenticare di includere using System.Collections; e using System.Collections.Generic;.