当我运行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编写者也不会写入失败的条目?
答案 0 :(得分:4)
检查aseertEquals()
实际执行的操作。最有可能它会抛出java.lang.AssertionError
,这是不是 java.lang.Exception
的子类。
这至少是JUnit的Assert.assert*()
方法所做的。
答案 1 :(得分:1)
在测试框架中,有一些监听器用于在断言后包含操作。
根据您使用的测试框架,有不同的实现。但我假设您使用JUnit。 (如果你可以更容易和更好地改为TestNG)
将此信用于JUnit Listener
的MEMORYNOTFOUNDJUnit Listener
这些是侦听器可以拦截测试结果的选项。这些包括许多信息,例如测试用例的名称。由于您要传递信息,因此必须将其添加到消息中,并在侦听器中解析所需的信息。
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();
}
}