【问题标题】:Assert try/catch block not working as intended断言 try/catch 块未按预期工作
【发布时间】:2016-01-23 02:29:30
【问题描述】:

当我运行我的 Selenium 脚本时,我使用的断言方法 sorta 有效。当满足断言(元素存在)时,脚本将写入 xls 文件。如果不满足,脚本不会写入 xls 文件,它会在此处停止测试。

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();
        }

那么,为什么即使我故意让我的测试失败,excel 编写器也不写失败条目?

【问题讨论】:

    标签: java excel selenium try-catch


    【解决方案1】:

    检查aseertEquals() 的实际作用。很可能它会抛出java.lang.AssertionError,它是java.lang.Exceptionnot 子类。

    这至少是 JUnit 的 Assert.assert*() 方法所做的。

    【讨论】:

    • 在那种情况下我必须赶上AssertionError,对吧?我的理解是,try/catch 有点像 if/else,只有 catch 更适合异常,什么不是。如果我错了,请纠正我。
    • 那行得通,是的。不过,将其用作 if/else 并不是一个好主意!它适用于需要中止执行并采取纠正措施的特殊情况。此外,与 if/else 相比,抛出和捕获异常非常慢 - 慢 100 倍。 stackoverflow.com/questions/299068/how-slow-are-java-exceptions
    • 在你的例子中我肯定会使用 if/else。
    【解决方案2】:

    在测试框架中,您可以使用侦听器在断言之后包含操作。

    根据您使用的测试框架,有不同的实现。但我假设你使用 JUnit。 (如果你能改成TestNG,那就更简单更好了)

    将此归功于JUnit Listener的MEMORYNOTFOUND

    JUnit 监听器

    这些是侦听器可以拦截测试结果的选项。其中包括很多信息,例如测试用例的名称。既然要传递信息,就必须将其添加到消息中,并在侦听器中解析您需要的信息。

    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 必须将侦听器添加到测试执行中。

    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);
        }
    }
    

    JUnit 测试用例

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

    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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多