2015-06-14 20 views
5

Ciao, sono nuovo in Nunit e sto passando una serie di oggetti a un TestCase come TestCaseSource. Per qualche motivo comunque Nunit sembra eseguire il test prima senza parametri passati ad esso che si traduce in una produzione ignorata:Nunit esegue TestCase con un TestCaseSource con la prima iterazione senza parametri? Perché?

La prova:

private readonly object[] _nunitIsWeird = 
{ 
    new object[] {new List<string>{"one", "two", "three"}, 3}, 
    new object[] {new List<string>{"one", "two"}, 2} 

}; 

[TestCase, TestCaseSource("_nunitIsWeird")] 
public void TheCountsAreCorrect(List<string> entries, int expectedCount) 
{ 
    Assert.AreEqual(expectedCount,Calculations.countThese(entries)); 
} 

TheCountsAreCorrect (3 prove), non riuscita: Uno o più test bambino aveva errori TheCountsAreCorrect(), Ignorato: Non sono argomenti sono stati forniti TheCountsAreCorrect (System.Collections.Generic.List 1[System.String],2), Success TheCountsAreCorrect(System.Collections.Generic.List 1 [System.String], 3), Successo

Quindi il primo test viene ignorato perché non ci sono parametri, ma non voglio che questo test venga eseguito, mai, non ha senso e sta rovinando il mio output di test. Ho provato a ignorarlo e questo imposta correttamente l'output del test, ma ritorna quando eseguo nuovamente tutti i test.

C'è qualcosa che mi manca, ho cercato ovunque.

risposta

6

TestCase e TestCaseSource fare due cose diverse. Hai solo bisogno di rimuovere l'attributo TestCase.

[TestCaseSource("_nunitIsWeird")] 
public void TheCountsAreCorrect(List<string> entries, int expectedCount) 
{ 
    Assert.AreEqual(expectedCount,Calculations.countThese(entries)); 
} 

L'attributo TestCase è per la fornitura di dati in linea, in modo NUnit tenta di fornire parametri di prova, che sta fallendo. Quindi sta elaborando l'attributo TestCaseSource e sta cercando i dati che fornisce e sta tentando di passarlo anche al test, che funziona correttamente.

Come nota a margine, a rigor di termini, i documenti suggeriscono che si dovrebbe anche segnare il test TestCaseSource con un attributo Test come qui di seguito, ma non ho mai trovato questo necessarie:

[Test, TestCaseSource("_nunitIsWeird")] 
public void TheCountsAreCorrect(List<string> entries, int expectedCount) 
+0

Grazie forsvarir, è stato la mia lettura errata del testo che significava che stavo usando TastCase invece di Test. Grazie – Phil