我正在运行由电子表格驱动的数据驱动测试,这些测试输入到TestNG测试框架。简而言之,框架遍历outlook邮箱并根据主题查找邮件。电子表格中的每一行都是邮件主题,它是测试的输入。
String comparisonResult = GetMail.compareMails(scenarioName);
Assert.assertNotNull(comparisonResult,"Mail does not exist");
在幕后,comparemails
方法将作为参数传递的邮件主题与方法进行比较,并在邮箱中查找该邮件。
当邮件不存在并且使用断言消息报告时,我声明为空值 - “邮箱中不存在邮件”。但是,在每次测试失败后都不显示断言消息,而是报告所有测试运行结束,即在处理完电子表格的所有行之后。
当断言失败时,我期望它立即在控制台上打印断言消息并从电子表格处理下一个测试。
但是,它会在测试结束时一次打印消息,如下所示,假设有5次失败:
[name=null](com.dms.testsuite.Test) Time elapsed: 66.905 sec <<< FAILURE!
java.lang.AssertionError: Mail does not exist expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:404)
at com.dms.testsuite.Test.submitJob(Test.java:110)
[name=null](com.dms.testsuite.Test) Time elapsed: 83.24 sec <<< FAILURE!
java.lang.AssertionError: Mail does not exist expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:404)
at com.dms.testsuite.Test.submitJob(Test.java:110)
[name=null](com.dms.testsuite.Test) Time elapsed: 51.064 sec <<< FAILURE!
java.lang.AssertionError: Mail does not exist expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:404)
at com.dms.testsuite.Test.submitJob(Test.java:110)
[name=null](com.dms.testsuite.Test) Time elapsed: 51.735 sec <<< FAILURE!
java.lang.AssertionError: Mail does not exist expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:404)
at com.dms.testsuite.Test.submitJob(Test.java:110)
[name=null](com.dms.testsuite.Test) Time elapsed: 67.177 sec <<< FAILURE!
java.lang.AssertionError: Mail does not exist expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:404)
at com.dms.testsuite.Test.submitJob(Test.java:110)
结果报告如下:
Failed tests:
com.dms.testsuite.Test.[name=null](com.dms.testsuite.Test)
Run 1: Test.submitJob:110 Mail does not exist expected object to not be null
Run 2: PASS
Run 3: Test.submitJob:110 Mail does not exist expected object to not be null
Run 4: PASS
Run 5: Test.submitJob:110 Mail does not exist expected object to not be null
Run 6: PASS
Run 7: Test.submitJob:110 Mail does not exist expected object to not be null
Run 8: Test.submitJob:110 Mail does not exist expected object to not be null
编辑:添加班级代码:
public class SubmitJobTest extends BaseClass implements ITest {
private String name;
@BeforeSuite
public void generateXML() {
try {
XMLGenerator.mapper();
} catch (ValidityException e) {
e.printStackTrace();
} catch (ParsingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(dataProvider = "files", dataProviderClass = FolderReader.class)
public void submitJob(final String name) throws Exception {
final SubmitJobEndpoint submitJobObj = new SubmitJobEndpoint();
String submitJobId = null;
try {
final BaseClass obj = new BaseClass();
obj.getPropValues();
submitJobId = submitJobObj.submitJob(name.toString(), BaseClass.queue,
BaseClass.config, BaseClass.type);
System.out.println("Job Id : " + submitJobId);
Assert.assertNotNull(submitJobId, "Job creation failed");
} catch (final InvalidKeyException e) {
e.printStackTrace();
} catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (final ClientProtocolException e) {
e.printStackTrace();
} catch (final URISyntaxException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
final JobDetailEndpoint JobDetailObj = new JobDetailEndpoint();
String status = null;
while (true) {
Thread.sleep(15000);
status = JobDetailObj.GetJob(submitJobId);
if (status.equalsIgnoreCase("FINISHED")
|| status.equalsIgnoreCase("FAILED")
|| status.equalsIgnoreCase("SPOOLED")) {
break;
}
System.out.println("Output :: " + status);
}
if (status.equalsIgnoreCase("SPOOLED")) {
Assert.assertEquals(status.toUpperCase(), "SPOOLED",
"Email creation failed");
} else {
Assert.assertEquals(status.toUpperCase(), "FINISHED",
"Email creation failed");
Map<String, List<String>> Scenario = ExcelReader.excelReader();
String scenarioName = FilenameUtils.removeExtension(name);
Assert.assertNotNull(Scenario.get(scenarioName),
"Test is not enabled in xls");
if (Scenario.get(scenarioName).get(1).equalsIgnoreCase("email")) {
Double comparisonResult = GetMail.compareMails(scenarioName);
Assert.assertNotNull(comparisonResult,
"GeneratedMail or BaselineMail does not exist | ");
Assert.assertTrue((comparisonResult) == 100,
"Generated mail content is not the same | ");
}
}
}
修改:数据提供商代码
public class FolderReader {
@DataProvider(name = "files")
public static Object[][] GetFileNames() {
final File folder = new File("//Testing_XML_source");
final File[] listOfFiles = folder.listFiles();
final List<Object[]> list = new ArrayList<Object[]>();
for (final File file : listOfFiles) {
if (file.getName().toString().endsWith(".xml")) {
// System.out.println("File : " + file.getName().toString());
list.add(new Object[] {file.getName().toString()});
}
}
return list.toArray(new Object[list.size()][]);
}
}
编辑:基类代码
public class BaseClass {
public static String queue;
public static String config;
public static String type;
InputStream inputStream;
public void getPropValues() throws IOException {
try {
final Properties prop = new Properties();
final String propFileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException(
"property file '" + propFileName + "' not found in the classpath");
}
// get the property value and print it out
BaseClass.queue = prop.getProperty("queue");
BaseClass.config = prop.getProperty("config");
BaseClass.type = prop.getProperty("type");
} catch (final Exception e) {
System.out.println("Exception: " + e);
} finally {
inputStream.close();
}
}
}