2011-11-23 21 views
5

Desidero un TestMethod per più eccezioni. Il problema è che il metodo Test si interrompe dopo la prima eccezione generata.UnitTest ExpectedException con più eccezioni

so che posso fare qualcosa di simile:

try 
{ 
    sAbc.ToInteger(); 
    Assert.Fail(); // If it gets to this line, no exception was thrown 
} 
catch (ArgumentException) { } 

ma voglio utilizzare il seguente codice-base:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); // throws an exception and stops here 
    sDecimal.ToInteger(); // throws theoretically a exception too... 
} 

E non voglio creare una TestMethod per ogni possibile eccezione del genere:

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sAbc.ToInteger(); 
} 

[TestMethod, ExpectedException(typeof(ArgumentException), "...")] 
public void StringToIntException() 
{ 
    sDecimal.ToInteger(); 
} 

risposta

0

per quanto ne so, è impossibile con gli attributi, perché quando si exce viene lanciata, l'esecuzione dei metodi viene interrotta. Pertanto se hai un'eccezione nella prima riga, la seconda non verrà eseguita.

Se si ha realmente bisogno la funzione, utilizzare NUnit che ha:

Assert.Throws<Exception>(delegate { /*Your code*/ }); 
+0

come su o di tipo più eccezioni? solo una eccezione accadrà alla volta. Questo è raggiungibile da parte di Mstest? – liang