下面列出了怎么用org.testng.xml.XmlSuite的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
ExtentService.getInstance().setReportUsesManualConfiguration(true);
ExtentService.getInstance().setAnalysisStrategy(AnalysisStrategy.SUITE);
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = r.getTestContext();
ExtentTest suiteTest = ExtentService.getInstance().createTest(r.getTestContext().getSuite().getName());
buildTestNodes(suiteTest, context.getFailedTests(), Status.FAIL);
buildTestNodes(suiteTest, context.getSkippedTests(), Status.SKIP);
buildTestNodes(suiteTest, context.getPassedTests(), Status.PASS);
}
}
for (String s : Reporter.getOutput()) {
ExtentService.getInstance().setTestRunnerOutput(s);
}
ExtentService.getInstance().flush();
}
/**
* generateReport method
*
* @param xmlSuites
* @param suites
* @param outputDirectory
*/
@Override
public void generateReport(List<XmlSuite> xmlSuites,
List<ISuite> suites,
String outputDirectory) {
for (ISuite suite : suites) {
init(suite);
Map<String, ISuiteResult> results = suite.getResults();
for ( ISuiteResult result : results.values() ) {
try {
processTestResults(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
extent.flush();
}
protected void resetSingleXmlSuite(XmlSuite suite) {
for (XmlTest xmlTest : suite.getTests()) {
for (XmlClass xmlClass : xmlTest.getClasses()) {
Class testClass = xmlClass.getSupportClass();
if (testClass.getAnnotation(TestNGOnArk.class) != null) {
if (!DelegateArkContainer.isStarted()) {
DelegateArkContainer.launch(testClass);
}
try {
xmlClass.setClass(DelegateArkContainer.getTestClassLoader().loadClass(
testClass.getCanonicalName()));
} catch (ClassNotFoundException ex) {
throw new ArkRuntimeException(String.format(
"Load testNG test class %s failed.", testClass.getCanonicalName()), ex);
}
}
}
}
}
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
ExtentService.getInstance().setReportUsesManualConfiguration(true);
ExtentService.getInstance().setAnalysisStrategy(AnalysisStrategy.SUITE);
for (ISuite suite : suites) {
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values()) {
ITestContext context = r.getTestContext();
ExtentTest suiteTest = ExtentService.getInstance().createTest(r.getTestContext().getSuite().getName());
buildTestNodes(suiteTest, context.getFailedTests(), Status.FAIL);
buildTestNodes(suiteTest, context.getSkippedTests(), Status.SKIP);
buildTestNodes(suiteTest, context.getPassedTests(), Status.PASS);
}
}
for (String s : Reporter.getOutput()) {
ExtentService.getInstance().setTestRunnerOutput(s);
}
ExtentService.getInstance().flush();
}
@DataProvider(name = "parallelConfiguration")
public static Object[][] parallelConfiguration() {
return new Object[][]{
new Object[]{XmlSuite.ParallelMode.NONE, 10},
new Object[]{XmlSuite.ParallelMode.NONE, 5},
new Object[]{XmlSuite.ParallelMode.NONE, 2},
new Object[]{XmlSuite.ParallelMode.NONE, 1},
new Object[]{XmlSuite.ParallelMode.METHODS, 10},
new Object[]{XmlSuite.ParallelMode.METHODS, 5},
new Object[]{XmlSuite.ParallelMode.METHODS, 2},
new Object[]{XmlSuite.ParallelMode.METHODS, 1},
new Object[]{XmlSuite.ParallelMode.CLASSES, 10},
new Object[]{XmlSuite.ParallelMode.CLASSES, 5},
new Object[]{XmlSuite.ParallelMode.CLASSES, 2},
new Object[]{XmlSuite.ParallelMode.CLASSES, 1},
new Object[]{XmlSuite.ParallelMode.INSTANCES, 10},
new Object[]{XmlSuite.ParallelMode.INSTANCES, 5},
new Object[]{XmlSuite.ParallelMode.INSTANCES, 2},
new Object[]{XmlSuite.ParallelMode.INSTANCES, 1},
new Object[]{XmlSuite.ParallelMode.TESTS, 10},
new Object[]{XmlSuite.ParallelMode.TESTS, 5},
new Object[]{XmlSuite.ParallelMode.TESTS, 2},
new Object[]{XmlSuite.ParallelMode.TESTS, 1},
};
}
@SuppressWarnings("unchecked")
@AllureFeatures.Descriptions
@Test(description = "Javadoc description of befores", dataProvider = "parallelConfiguration")
public void descriptionsBefores(final XmlSuite.ParallelMode mode, final int threadCount) {
final String beforeClassDescription = "Before class description";
final String beforeMethodDescription = "Before method description";
final AllureResults results = runTestNgSuites(
parallel(mode, threadCount),
"suites/descriptions-test.xml"
);
final List<TestResultContainer> testContainers = results.getTestResultContainers();
assertThat(testContainers).as("Test containers has not been written")
.isNotEmpty()
.filteredOn(container -> !container.getBefores().isEmpty())
.extracting(container -> container.getBefores().get(0).getDescriptionHtml().trim())
.as("Javadoc description of befores have not been processed")
.containsOnly(beforeClassDescription, beforeMethodDescription);
}
@AllureFeatures.Fixtures
@Test(description = "Suite fixtures", dataProvider = "parallelConfiguration")
public void perSuiteFixtures(final XmlSuite.ParallelMode mode, final int threadCount) {
String suiteName = "Test suite 12";
String testTagName = "Test tag 12";
String before1 = "beforeSuite1";
String before2 = "beforeSuite2";
String after1 = "afterSuite1";
String after2 = "afterSuite2";
final AllureResults results = runTestNgSuites(
parallel(mode, threadCount),
"suites/per-suite-fixtures-combination.xml"
);
List<TestResult> testResult = results.getTestResults();
List<TestResultContainer> testContainers = results.getTestResultContainers();
assertThat(testResult).as("Unexpected quantity of testng case results has been written").hasSize(1);
List<String> testUuid = singletonList(testResult.get(0).getUuid());
assertContainersChildren(testTagName, testContainers, testUuid);
assertContainersChildren(suiteName, testContainers, getUidsByName(testContainers, testTagName));
assertBeforeFixtures(suiteName, testContainers, before1, before2);
assertAfterFixtures(suiteName, testContainers, after1, after2);
}
@AllureFeatures.Fixtures
@Test(description = "Test fixtures", dataProvider = "parallelConfiguration")
public void perTestTagFixtures(final XmlSuite.ParallelMode mode, final int threadCount) {
String suiteName = "Test suite 13";
String testTagName = "Test tag 13";
String before1 = "beforeTest1";
String before2 = "beforeTest2";
String after1 = "afterTest1";
String after2 = "afterTest2";
final AllureResults results = runTestNgSuites(
parallel(mode, threadCount),
"suites/per-test-tag-fixtures-combination.xml"
);
List<TestResult> testResult = results.getTestResults();
List<TestResultContainer> testContainers = results.getTestResultContainers();
assertThat(testResult).as("Unexpected quantity of testng case results has been written").hasSize(1);
List<String> testUuid = singletonList(testResult.get(0).getUuid());
assertContainersChildren(testTagName, testContainers, testUuid);
assertContainersChildren(suiteName, testContainers, getUidsByName(testContainers, testTagName));
assertBeforeFixtures(testTagName, testContainers, before1, before2);
assertAfterFixtures(testTagName, testContainers, after1, after2);
}
@SuppressWarnings("unchecked")
@AllureFeatures.Fixtures
@Issue("67")
@Test(description = "Should set correct status for failed after fixtures")
public void shouldSetCorrectStatusForFailedAfterFixtures() {
final Consumer<TestNG> configurer = parallel(XmlSuite.ParallelMode.METHODS, 5);
final AllureResults results = runTestNgSuites(
configurer,
"suites/failed-after-suite-fixture.xml",
"suites/failed-after-test-fixture.xml",
"suites/failed-after-method-fixture.xml"
);
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getAfters)
.hasSize(3)
.extracting(FixtureResult::getName, FixtureResult::getStatus)
.containsExactlyInAnyOrder(
Tuple.tuple("afterSuite", Status.BROKEN),
Tuple.tuple("afterTest", Status.BROKEN),
Tuple.tuple("afterMethod", Status.BROKEN)
);
}
@SuppressWarnings("unchecked")
@AllureFeatures.Fixtures
@Issue("304")
@Test(dataProvider = "parallelConfiguration")
public void shouldProcessFailedSetUps(final XmlSuite.ParallelMode mode, final int threadCount) {
final AllureResults results = runTestNgSuites(parallel(mode, threadCount), "suites/gh-304.xml");
assertThat(results.getTestResults())
.extracting(TestResult::getName, TestResult::getStatus)
.contains(tuple("skippedTest", Status.SKIPPED));
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getAfters)
.extracting(FixtureResult::getName, FixtureResult::getStatus)
.contains(tuple("afterAlways", Status.PASSED));
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getAfters)
.filteredOn("name", "afterAlways")
.flatExtracting(FixtureResult::getSteps)
.extracting(StepResult::getName)
.containsExactly(
"first", "second"
);
}
@Override
public void generateReport(
List<XmlSuite> arg0,
List<ISuite> arg1,
String arg2 ) {
//we just need the report format, that why we set other fields null
ReportFormatter reportFormatter = new ReportFormatter(ReportAppender.getRuns(),
mailSubjectFormat,
null,
null,
0,
null);
// send report by mail
MailReportSender mailReportSender = new MailReportSender(reportFormatter.getDescription(),
reportFormatter.toHtml());
mailReportSender.send();
}
@Override
public void alter(List<XmlSuite> suites) {
List<XmlSuite> cloned = new ArrayList<>(suites);
suites.clear();
for (XmlSuite suite : cloned) {
String logLevels = suite.getParameter(LOG_LEVELS_PARAM_NAME);
if (StringUtils.isNotEmpty(logLevels)) {
List<XmlSuite> newSuites = createSuites(logLevels, suite);
if (!newSuites.isEmpty()) {
suites.addAll(newSuites);
}
} else {
suites.add(suite);
}
}
}
protected ITestResult prepareMock(Class<?> tClass, Method method) {
ITestResult result = mock(ITestResult.class);
IClass clazz = mock(IClass.class);
ITestNGMethod testNGMethod = mock(ITestNGMethod.class);
ConstructorOrMethod cm = mock(ConstructorOrMethod.class);
String methodName = method.getName();
when(result.getTestClass()).thenReturn(clazz);
when(result.getTestClass().getRealClass()).thenReturn(tClass);
when(clazz.getName()).thenReturn(this.getClass().getName());
when(result.getMethod()).thenReturn(testNGMethod);
when(cm.getMethod()).thenReturn(method);
when(result.getMethod().getConstructorOrMethod()).thenReturn(cm);
when(testNGMethod.getMethodName()).thenReturn(methodName);
ITestContext context = mock(ITestContext.class);
when(result.getTestContext()).thenReturn(context);
XmlTest xmlTest = new XmlTest();
XmlSuite suite = new XmlSuite();
xmlTest.setXmlSuite(suite);
suite.setListeners(Arrays.asList(VideoListener.class.getName()));
when(context.getCurrentXmlTest()).thenReturn(xmlTest);
return result;
}
public XmlSuite constructXmlSuiteForDistribution(List<String> tests,
Map<String, List<Method>> methods,
String suiteName,
String category,
int deviceCount) {
XmlSuite suite = new XmlSuite();
suite.setName(suiteName);
suite.setThreadCount(deviceCount);
suite.setParallel(ParallelMode.CLASSES);
suite.setVerbose(2);
listeners.add("com.appium.manager.AppiumParallelMethodTestListener");
listeners.add("com.appium.utils.RetryListener");
include(listeners, LISTENERS);
suite.setListeners(listeners);
XmlTest test = new XmlTest(suite);
test.setName(category);
test.addParameter("device", "");
include(groupsExclude, EXCLUDE_GROUPS);
include(groupsInclude, INCLUDE_GROUPS);
test.setIncludedGroups(groupsInclude);
test.setExcludedGroups(groupsExclude);
List<XmlClass> xmlClasses = writeXmlClass(tests, methods);
test.setXmlClasses(xmlClasses);
writeTestNGFile(suite);
return suite;
}
public XmlSuite constructXmlSuiteForParallelCucumber(
int deviceCount, List<AppiumDevice> deviceSerail) {
ArrayList<String> listeners = new ArrayList<>();
listeners.add("com.cucumber.listener.CucumberListener");
XmlSuite suite = new XmlSuite();
suite.setName("TestNG Forum");
suite.setThreadCount(deviceCount);
suite.setParallel(ParallelMode.TESTS);
suite.setVerbose(2);
suite.setListeners(listeners);
for (int i = 0; i < deviceCount; i++) {
XmlTest test = new XmlTest(suite);
test.setName("TestNG Test" + i);
test.setPreserveOrder(false);
test.addParameter("device", deviceSerail.get(i).getDevice().getUdid());
test.setPackages(getPackages());
}
return getXmlSuite(suite);
}
private void executeInCurrentLoader(final ResultCollector rc) {
final TestNGAdapter listener = new TestNGAdapter(this.clazz,
this.getDescription(), rc);
final XmlSuite suite = createSuite();
TESTNG.setDefaultSuiteName(suite.getName());
TESTNG.setXmlSuites(Collections.singletonList(suite));
LISTENER.setChild(listener);
try {
TESTNG.run();
} finally {
// yes this is hideous
LISTENER.setChild(null);
}
}
/**
* Adds the entries from the properties document to the set of test suite
* parameters. An entry is skipped if its value is an empty string.
*/
@Override
public void alter(List<XmlSuite> xmlSuites) {
if (null == this.testRunArgs || this.testRunArgs.getElementsByTagName("entry").getLength() == 0) {
return;
}
for (XmlSuite xmlSuite : xmlSuites) {
Map<String, String> params = xmlSuite.getParameters();
NodeList entries = this.testRunArgs.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
Element entry = (Element) entries.item(i);
String value = entry.getTextContent().trim();
if (value.isEmpty()) {
continue;
}
params.put(entry.getAttribute("key"), value);
LOGR.log(Level.FINE, "Added parameter: {0}={1}", new Object[] { entry.getAttribute("key"), value });
}
params.put("uuid", this.testRunId.toString());
}
}
protected String getTitle(XmlSuite suite) {
String browser = getBrowser();
if (!browser.isEmpty()) {
browser = " " + browser; // insert the space before
}
String device = getDeviceName();
String env = !Configuration.isNull(Parameter.ENV) ? Configuration.get(Parameter.ENV)
: Configuration.get(Parameter.URL);
String title = "";
String app_version = "";
if (!Configuration.get(Parameter.APP_VERSION).isEmpty()) {
// if nothing is specified then title will contain nothing
app_version = Configuration.get(Parameter.APP_VERSION) + " - ";
}
String suiteName = getSuiteName(suite);
String xmlFile = getSuiteFileName(suite);
title = String.format(SUITE_TITLE, app_version, suiteName, String.format(XML_SUITE_NAME, xmlFile), env, device,
browser);
return title;
}
public void generateReport(List<XmlSuite> list, List<ISuite> list1, String s) {
if (getSystemInfo() != null) {
for (Map.Entry<String, String> entry : getSystemInfo().entrySet()) {
reporter.setSystemInfo(entry.getKey(), entry.getValue());
}
}
reporter.setTestRunnerOutput(testRunnerOutput);
reporter.flush();
}
public void generateReport(List<XmlSuite> list, List<ISuite> list1, String s) {
if (getSystemInfo() != null) {
for (Map.Entry<String, String> entry : getSystemInfo().entrySet()) {
reporter.setSystemInfo(entry.getKey(), entry.getValue());
}
}
reporter.setTestRunnerOutput(testRunnerOutput);
reporter.flush();
}
public void generateReport(List<XmlSuite> list, List<ISuite> list1, String s) {
if (getSystemInfo() != null) {
for (Map.Entry<String, String> entry : getSystemInfo().entrySet()) {
reporter.setSystemInfo(entry.getKey(), entry.getValue());
}
}
reporter.setTestRunnerOutput(testRunnerOutput);
reporter.flush();
}
@Override
public void alter(List<XmlSuite> suites) {
for (XmlSuite xmlSuite : suites) {
resetXmlSuite(xmlSuite);
resetChildrenXmlSuite(xmlSuite.getChildSuites());
}
}
protected void resetXmlSuite(XmlSuite suite) {
if (suite == null) {
return;
}
resetXmlSuite(suite.getParentSuite());
resetSingleXmlSuite(suite);
}
protected void resetChildrenXmlSuite(List<XmlSuite> childSuites) {
if (childSuites.isEmpty()) {
return;
}
for (XmlSuite xmlSuite : childSuites) {
resetChildrenXmlSuite(xmlSuite.getChildSuites());
resetSingleXmlSuite(xmlSuite);
}
}
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
StringBuilder sBuilder = new StringBuilder();
// Iterating over each suite
for (ISuite suite : suites) {
// Getting the results for the each suite
Map<String, ISuiteResult> suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
// get context for result
ITestContext tc = sr.getTestContext();
// go over all cases
for (ITestNGMethod method : tc.getAllTestMethods()) {
// if a case has "ExpectedFaiGPDBWritable.javalure" annotation, insert to sBuilder
if (method.getConstructorOrMethod().getMethod().getAnnotation(ExpectedFailure.class) != null) {
sBuilder.append(method.getInstance().getClass().getName() + "/" + method.getMethodName()).append(System.lineSeparator());
}
}
}
}
// write results to file
try {
FileUtils.writeStringToFile(new File(outputDirectory + "/automation_expected_failures.txt"), sBuilder.toString(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
@AllureFeatures.Base
@Test(description = "Test with timeout", dataProvider = "parallelConfiguration")
public void testWithTimeout(final XmlSuite.ParallelMode mode, final int threadCount) {
final String testNameWithTimeout = "testWithTimeout";
final String testNameWithoutTimeout = "testWithoutTimeout";
final AllureResults results = runTestNgSuites(parallel(mode, threadCount), "suites/tests-with-timeout.xml");
List<TestResult> testResults = results.getTestResults();
assertThat(testResults)
.as("Test case results have not been written")
.hasSize(2)
.as("Unexpectedly passed status or stage of tests")
.allMatch(testResult -> testResult.getStatus().equals(Status.PASSED) &&
testResult.getStage().equals(Stage.FINISHED))
.extracting(TestResult::getName)
.as("Unexpectedly passed name of tests")
.containsOnlyElementsOf(asList(
testNameWithoutTimeout,
testNameWithTimeout)
);
assertThat(testResults)
.flatExtracting(TestResult::getSteps)
.as("No steps present for test with timeout")
.hasSize(2)
.extracting(StepResult::getName)
.containsOnlyElementsOf(asList(
"Step of the test with timeout",
"Step of the test with no timeout")
);
}
@AllureFeatures.Fixtures
@Test(description = "Class fixtures", dataProvider = "parallelConfiguration")
public void perClassFixtures(final XmlSuite.ParallelMode mode, final int threadCount) {
final AllureResults results = runTestNgSuites(
parallel(mode, threadCount),
"suites/per-class-fixtures-combination.xml"
);
assertThat(results.getTestResults())
.extracting(TestResult::getName)
.containsExactlyInAnyOrder("test1", "test2");
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getBefores)
.extracting(FixtureResult::getName)
.containsExactlyInAnyOrder("beforeClass");
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getAfters)
.extracting(FixtureResult::getName)
.containsExactlyInAnyOrder("afterClass");
final TestResult test1 = findTestResultByName(results, "test1");
final TestResult test2 = findTestResultByName(results, "test2");
assertThat(results.getTestResultContainers())
.flatExtracting(TestResultContainer::getChildren)
.contains(test1.getUuid(), test2.getUuid());
}
@AllureFeatures.Fixtures
@Test(description = "Method fixtures", dataProvider = "parallelConfiguration")
public void perMethodFixtures(final XmlSuite.ParallelMode mode, final int threadCount) {
String suiteName = "Test suite 11";
String testTagName = "Test tag 11";
String before1 = "io.qameta.allure.testng.samples.PerMethodFixtures.beforeMethod1";
String before2 = "io.qameta.allure.testng.samples.PerMethodFixtures.beforeMethod2";
String after1 = "io.qameta.allure.testng.samples.PerMethodFixtures.afterMethod1";
String after2 = "io.qameta.allure.testng.samples.PerMethodFixtures.afterMethod2";
final AllureResults results = runTestNgSuites(
parallel(mode, threadCount),
"suites/per-method-fixtures-combination.xml"
);
List<TestResult> testResults = results.getTestResults();
List<TestResultContainer> testContainers = results.getTestResultContainers();
assertThat(testResults).as("Unexpected quantity of testng case results has been written").hasSize(2);
List<String> uuids = testResults.stream().map(TestResult::getUuid).collect(Collectors.toList());
assertContainersChildren(testTagName, testContainers, uuids);
assertContainersChildren(suiteName, testContainers, getUidsByName(testContainers, testTagName));
assertContainersPerMethod(before1, testContainers, uuids);
assertContainersPerMethod(before2, testContainers, uuids);
assertContainersPerMethod(after1, testContainers, uuids);
assertContainersPerMethod(after2, testContainers, uuids);
}
protected Consumer<TestNG> parallel(final XmlSuite.ParallelMode mode,
final int threadCount) {
return testNG -> {
testNG.setParallel(mode);
testNG.setThreadCount(threadCount);
};
}
/** Creates summary of the run */
@Override
public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {
try {
m_out = createWriter(outdir);
} catch (IOException e) {
L.error("output file", e);
return;
}
ConfigUtil cr = ConfigUtil.getInstance();
if (cr.getSrouceCodeEncoding() != null) {
builder.setEncoding(cr.getSrouceCodeEncoding());
} else {
builder.setEncoding("UTF-8");
}
builder.addSourceTree(new File(cr.getSourceCodeDir()));
startHtml(m_out);
generateSuiteSummaryReport(suites);
testIds.clear();
generateMethodSummaryReport(suites);
testIds.clear();
generateMethodDetailReport(suites);
testIds.clear();
endHtml(m_out);
m_out.flush();
m_out.close();
}