我试图在测试中跳过测试方法中的异常。使用数据提供程序从Excel获取数据并根据我想跳过测试的条件,我抛出跳过异常但这些测试用例也显示为已通过。我的代码如下:
@Test(dataProvider = "postProperty")
public static void postProperty(Object testCaseNo , Object propertyFor , Object testCaseFlag , Object propertyType , Object propertyCode){
int ppTestCaseNo = (int) testCaseNo;
String ppPropertyFor = propertyFor.toString();
String ppTestCaseFlag = testCaseFlag.toString().trim();
String ppPropertyType = propertyType.toString();
int ppPropertyCode = (int) (double) propertyCode;
boolean propertyIdFlag = true;
try {
if (ppTestCaseFlag.equalsIgnoreCase("Yes")){
GenericFunctions.excelDataForTestCase(ppTestCaseNo);
driver = new ChromeDriver();
Robot robot = new Robot();
String browserType = workSheet.getRow(ppTestCaseNo).getCell(5).getStringCellValue().trim();
switch (browserType) {
case "Deployment" :
driver.get("http://deployment.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "Live" :
driver.get("http://post.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "Staging" :
driver.get("http://staging.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "NewMB" :
driver.get("http://newMB.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
if (ppPropertyType.equalsIgnoreCase("Multistorey Apartment")){
boolean propertyIdPostedFlag = PostProperty.functionPostPropertyMultiStoreyApartment(ppPropertyType , ppPropertyCode ,driver , robot, ppTestCaseNo ,log);
Assert.assertEquals(propertyIdFlag, propertyIdPostedFlag);
}
else{
boolean propertyIdPostedFlag =PostProperty.functionPostPropertyOtherPropertyType(ppPropertyType , ppPropertyCode ,driver , robot, ppTestCaseNo ,log);
Assert.assertEquals(propertyIdFlag, propertyIdPostedFlag);
}
}
else if (ppTestCaseFlag.equalsIgnoreCase("No")){
throw new SkipException("Skipping test case for "+ppPropertyType);
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
您正在捕获catch(Exception ...)中的SkipException,只打印一个堆栈跟踪。因此testNG不知道抛出这个异常。
答案 1 :(得分:1)
实际上,在抛出Exception
后,您在整个代码中都会抓住SkipException
。
你知道Exception
是一个基类而SkipException
是它的孩子,所以如果你使用catch (Exception e)
,它将捕获你代码中抛出的所有异常。
因此,如果你想抛出SkipException
并捕获除此之外的所有异常,你需要捕获可能抛出代码而不是Exception
的单个子异常。
我给你举一些例子来捕捉一些可能存在于你的代码中的异常: -
try {
// Your code here
throw new SkipException("Skipping test case for "+ppPropertyType);
}catch (WebDriverException e){
}catch (IllegalStateException e){
}......