断言try / catch块不能按预期工作

当我运行我的Selenium脚本时,我使用了sorta的断言方法。 当断言得到满足(元素存在)时,脚本写入一个xls文件。 当它不符合时,脚本不写入xls文件,它停止在那里的testing。

try { assertEquals(driver.findElement(By.xpath(prop.getProperty("failedlogin"))).getText(), "LOG IN"); //add pass entry to the excel sheet testresultdata.put("4", new Object[] { 3d, "User should not be able to login with no password", "Login failed", "Pass" }); } catch (Exception e) { //add fail entry to the excel sheet testresultdata.put("4", new Object[] { 3d, "User should not be able to login with no password", "Login failed", "Fail" }); } 

这里是我的Excel作家方法:

 @BeforeClass(alwaysRun = true) public void setupBeforeSuite(ITestContext context) throws IOException { //create a new work book workbook = new HSSFWorkbook(); //create a new work sheet sheet = workbook.createSheet("Test Result"); testresultdata = new LinkedHashMap < String, Object[] > (); //add test result excel file column header //write the header in the first row testresultdata.put("1", new Object[] { "Test Step Id", "Action", "Expected Result", "Actual Result" }); } 

和:

 @AfterClass public void setupAfterSuite(ITestContext context) { //write excel file and file name is TestResult.xls Set < String > keyset = testresultdata.keySet(); int rownum = 0; for (String key: keyset) { Row row = sheet.createRow(rownum++); Object[] objArr = testresultdata.get(key); int cellnum = 0; for (Object obj: objArr) { Cell cell = row.createCell(cellnum++); if (obj instanceof Date) cell.setCellValue((Date) obj); else if (obj instanceof Boolean) cell.setCellValue((Boolean) obj); else if (obj instanceof String) cell.setCellValue((String) obj); else if (obj instanceof Double) cell.setCellValue((Double) obj); } } try { FileOutputStream out = new FileOutputStream(new File("C:/Users/matt_damon/workspace/project/passfail.xls")); workbook.write(out); out.close(); System.out.println("Excel written successfully.."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

那为什么呢,即使我故意让我的testing失败了,excel作者是不是写失败条目呢?

检查aseertEquals()实际上做了什么。 很可能它会抛出java.lang.AssertionError ,它不是 java.lang.Exception子类。

这至less是JUnit的Assert.assert*()方法。

在testing框架中,有一些侦听器用来在断言之后包含动作。

根据您使用的testing框架,有不同的实现。 但是我假设你使用JUnit。 (如果您可以更改为TestNG,更容易和更好)

在JUnit Listener中把它logging到MEMORYNOTFOUND

JUnit监听器

这些是监听器可以截取testing重新结果的选项。 这些包括很多信息,如testing用例的名称。 既然你想传递信息,你必须把它添加到消息中,并在侦听器中parsing你所需要的信息。

 package com.memorynotfound.test; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; public class JUnitExecutionListener extends RunListener { public void testRunStarted(Description description) throws Exception { System.out.println("Number of tests to execute: " + description.testCount()); } public void testRunFinished(Result result) throws Exception { System.out.println("Number of tests executed: " + result.getRunCount()); } public void testStarted(Description description) throws Exception { System.out.println("Starting: " + description.getMethodName()); } public void testFinished(Description description) throws Exception { System.out.println("Finished: " + description.getMethodName()); } //This function will print your message added to the assert public void testFailure(Failure failure) throws Exception { System.out.println("Failed: " + failure.getMessage()); } public void testAssumptionFailure(Failure failure) { System.out.println("Failed: " + failure.getDescription().getMethodName()); } public void testIgnored(Description description) throws Exception { System.out.println("Ignored: " + description.getMethodName()); } } 

JUnit Runner必须将侦听器添加到testing执行中。

 package com.memorynotfound.test; import org.junit.runner.notification.RunNotifier; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.InitializationError; public class MyRunner extends BlockJUnit4ClassRunner { public MyRunner(Class<?> klass) throws InitializationError { super(klass); } @Override public void run(RunNotifier notifier){ notifier.addListener(new JUnitExecutionListener()); super.run(notifier); } } 

JUnittesting用例

所有包含注解@RunWith的testing类都会激活监听器

 package com.memorynotfound.test; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertTrue; @RunWith(MyRunner.class) public class RunListenerTest { @Test public void testListener(){ } @Test public void testFalseAssertion(){ assertTrue("3d, User should not be able to login with no password, Login failed, Fail",false); } @Ignore @Test public void testIgnore(){ } @Test public void testException(){ throw new RuntimeException(); } } 
Interesting Posts