我有一个窗口窗体和一个按钮,我试图用codedUitest来测试它。如果引发任何异常,我希望测试失败,但在测试类中它不会捕获异常。如何捕获由另一个类抛出的测试类中的异常?
这里是我的代码:
public void button1_Click(object sender, EventArgs e)
{
double[] dummy = new double[1];
int i = 1;
if (i > 0)
{
throw new System.IndexOutOfRangeException("index parameter is out of range.");
}
else
{
dummy[i] = 6;
}
}
的测试方法是:
public void CodedUITestMethod1()
{
try
{
this.UIMap.TryBtn1();
}
catch (IndexOutOfRangeException)
{
Assert.Fail();
}
}
看到这个帖子在单位醒目异常测试 http://stackoverflow.com/questions/933613/how-do-i-use-assert-to-verify-that-an-exception-has-been-抛出 – Prashant
你使用什么单元测试框架? – xeraphim
http://stackoverflow.com/questions/8381042/catch-an-exception-thrown-by-another-form是好的,但我想在应用程序中使用它包含很多方法,它没有意义添加每个方法的try catch块,我使用.NET框架4.5 –