2011-11-23 11 views
5

मुझे एकाधिक अपवादों के लिए एक टेस्ट विधि चाहिए। समस्या यह है कि पहले फेंकने के अपवाद के बाद Testmethod बंद हो जाता है।एकाधिक अपवादों के साथ यूनिटटेस्ट अपेक्षित अपवाद

मुझे पता है कि मैं ऐसा ही कुछ कर सकते हैं:

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

लेकिन मैं निम्नलिखित कोड आधार का उपयोग करना चाहते:

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

और मैं के लिए एक testmethod बनाने के लिए नहीं करना चाहते हैं इस तरह प्रत्येक संभावित अपवाद:

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

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

उत्तर

0

जहाँ तक मुझे पता है, यह गुण के साथ असंभव है, क्योंकि जब आप उत्कृष्टता पशन फेंक दिया जाता है, विधियों का निष्पादन बाधित होता है। इसलिए यदि आपके पास पहली पंक्ति में अपवाद है, तो दूसरा निष्पादित नहीं किया जाएगा।

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

तरीके के बारे में या प्रकार कई अपवाद:

तुम सच में सुविधा की जरूरत है, NUnit जो है का उपयोग करें? एक समय में केवल एक अपवाद होगा। क्या यह mstest द्वारा प्राप्त किया जा सकता है? – liang

संबंधित मुद्दे