断言 try/catch 块未按预期工作
Assert try/catch block not working as intended
当我 运行 我的 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 作者也不写失败条目?
检查 aseertEquals()
实际做了什么。它很可能抛出 java.lang.AssertionError
,它是 java.lang.Exception
.
的 not 子类
至少 JUnit 的 Assert.assert*()
方法是这样做的。
在测试框架中,您可以使用侦听器在断言之后包含操作。
根据您使用的测试框架,有不同的实现。但我假设您使用 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 运行程序
必须将侦听器添加到测试执行中。
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();
}
}
当我 运行 我的 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 作者也不写失败条目?
检查 aseertEquals()
实际做了什么。它很可能抛出 java.lang.AssertionError
,它是 java.lang.Exception
.
至少 JUnit 的 Assert.assert*()
方法是这样做的。
在测试框架中,您可以使用侦听器在断言之后包含操作。
根据您使用的测试框架,有不同的实现。但我假设您使用 JUnit。 (如果你能换成TestNG,就更简单更好了)
将此归功于 JUnit Listener
处的 MEMORYNOTFOUNDJUnit 侦听器
这些是侦听器可以拦截测试结果的选项。这些包括很多信息,例如测试用例的名称。既然要传递信息,就得在消息中加上,在监听器中解析自己需要的信息。
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 运行程序 必须将侦听器添加到测试执行中。
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();
}
}