下面列出了怎么用org.testng.ITestNGListener的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
@Ignore("Fails against TestNG 6.11")
public void runTestAndAssertCounters() throws Exception {
TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
TestNG testNG = new TestNG();
testNG.addListener((ITestNGListener) listener);
testNG.setTestClasses(new Class<?>[] {this.clazz});
testNG.setVerbose(0);
testNG.run();
String name = this.clazz.getSimpleName();
assertEquals("tests started for [" + name + "] ==> ", this.expectedTestStartCount, listener.testStartCount);
assertEquals("successful tests for [" + name + "] ==> ", this.expectedTestSuccessCount, listener.testSuccessCount);
assertEquals("failed tests for [" + name + "] ==> ", this.expectedFailureCount, listener.testFailureCount);
assertEquals("failed configurations for [" + name + "] ==> ",
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = expectedTestCount;
final int expectedTestFinishedCount = expectedTestCount;
final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
final TestNG testNG = new TestNG();
testNG.addListener((ITestNGListener) listener);
testNG.setTestClasses(new Class<?>[] { testClass });
testNG.setVerbose(0);
testNG.run();
assertEquals("Failures for test class [" + testClass + "].", expectedTestFailureCount,
listener.testFailureCount);
assertEquals("Tests started for test class [" + testClass + "].", expectedTestStartedCount,
listener.testStartCount);
assertEquals("Successful tests for test class [" + testClass + "].", expectedTestFinishedCount,
listener.testSuccessCount);
}
@Test
@Ignore("Fails against TestNG 6.11")
public void runTestAndAssertCounters() throws Exception {
TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
TestNG testNG = new TestNG();
testNG.addListener((ITestNGListener) listener);
testNG.setTestClasses(new Class<?>[] {this.clazz});
testNG.setVerbose(0);
testNG.run();
String name = this.clazz.getSimpleName();
assertEquals("tests started for [" + name + "] ==> ", this.expectedTestStartCount, listener.testStartCount);
assertEquals("successful tests for [" + name + "] ==> ", this.expectedTestSuccessCount, listener.testSuccessCount);
assertEquals("failed tests for [" + name + "] ==> ", this.expectedFailureCount, listener.testFailureCount);
assertEquals("failed configurations for [" + name + "] ==> ",
this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) {
final int expectedTestFailureCount = 0;
final int expectedTestStartedCount = expectedTestCount;
final int expectedTestFinishedCount = expectedTestCount;
final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
final TestNG testNG = new TestNG();
testNG.addListener((ITestNGListener) listener);
testNG.setTestClasses(new Class<?>[] { testClass });
testNG.setVerbose(0);
testNG.run();
assertEquals("Failures for test class [" + testClass + "].", expectedTestFailureCount,
listener.testFailureCount);
assertEquals("Tests started for test class [" + testClass + "].", expectedTestStartedCount,
listener.testStartCount);
assertEquals("Successful tests for test class [" + testClass + "].", expectedTestFinishedCount,
listener.testSuccessCount);
}
protected List<ITestNGListener> initialiseListeners() {
List<ITestNGListener> listeners = new LinkedList<>();
String file = "META-INF/services/listeners.txt";
InputStream stream = getStream(file);
if (stream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
String line;
while ((line = reader.readLine()) != null) {
listeners.add(instantiate(line));
}
} catch (IOException e) {
throw new TestNGException(e);
}
}
return listeners;
}
public static void main(String[] args) throws Exception {
int count = -1;
Stopwatch sw = Stopwatch.createStarted();
while (++count<100) {
log.info("new test run\n\n\nTEST RUN "+count+"\n");
// ElectPrimaryTest t = new ElectPrimaryTest();
// t.setUp();
// t.testFireCausesPromoteDemote();
// t.tearDown();
TestNG testNG = new TestNG();
testNG.setTestClasses(new Class[] { ElectPrimaryTest.class });
testNG.addListener((ITestNGListener)new LoggingVerboseReporter());
FailedReporter failedReporter = new FailedReporter();
testNG.addListener((ITestNGListener)failedReporter);
testNG.run();
if (!failedReporter.getFailedTests().isEmpty()) {
log.error("Failures: "+failedReporter.getFailedTests());
System.exit(1);
}
}
log.info("\n\nCompleted "+count+" runs in "+Duration.of(sw));
}
private static TestNG run(Class[] testClasses, List<Class<? extends ITestNGListener>> listenerClasses) {
TestNG testNG = new TestNG();
testNG.setTestClasses(testClasses);
testNG.setListenerClasses(listenerClasses);
testNG.setUseDefaultListeners(false); // 不用默认监听器,不会自动生成testng的默认报告
testNG.run();
return testNG;
}
/**
* Get the listener of the specified type that's attached to the listener chain.
*
* @param <T> listener type
* @param listenerType listener type
* @return listener of the specified type
*/
public static <T extends ITestNGListener> T getLinkedListener(final Class<T> listenerType) {
ITestResult testResult = Reporter.getCurrentTestResult();
Optional<T> optListener =
ListenerChain.getAttachedListener(testResult, listenerType);
if (optListener.isPresent()) {
return optListener.get();
}
throw new IllegalStateException(listenerType.getSimpleName() + " listener wasn't found on the listener chain");
}
@Step("Run testng suites {suites}")
private AllureResults runTestNgSuites(final Consumer<TestNG> configurer,
final String... suites) {
final ClassLoader classLoader = getClass().getClassLoader();
List<String> suiteFiles = Arrays.stream(suites)
.map(classLoader::getResource)
.filter(Objects::nonNull)
.map(URL::getFile)
.collect(Collectors.toList());
assertThat(suites)
.as("Cannot find all suite xml files")
.hasSameSizeAs(suiteFiles);
final AllureResultsWriterStub results = new AllureResultsWriterStub();
final AllureLifecycle lifecycle = new AllureLifecycle(results);
final AllureTestNg adapter = new AllureTestNg(lifecycle);
final TestNG testNg = new TestNG(false);
testNg.addListener((ITestNGListener) adapter);
testNg.setTestSuites(suiteFiles);
configurer.accept(testNg);
final AllureLifecycle cached = Allure.getLifecycle();
try {
Allure.setLifecycle(lifecycle);
StepsAspects.setLifecycle(lifecycle);
AttachmentsAspects.setLifecycle(lifecycle);
testNg.run();
} finally {
Allure.setLifecycle(cached);
StepsAspects.setLifecycle(cached);
AttachmentsAspects.setLifecycle(cached);
}
return results;
}
@SuppressWarnings("unchecked")
static <T> T run(final String listenerClass, final Class<?>... testClasses) {
final TestNG tng = create(testClasses);
final InvokedMethodNameListener listener = new InvokedMethodNameListener();
final DataProviderTransformer dataProviderTransformer = new DataProviderTransformer();
tng.addListener((ITestNGListener) listener);
tng.setDefaultTestName("DataSupplier tests");
tng.run();
return (T) (listenerClass.equals(InvokedMethodNameListener.class.getName()) ? listener : dataProviderTransformer);
}
protected void queueBeforeInvocationListeners(IInvokedMethod iInvokedMethod,
ITestResult iTestResult,
List<ITestNGListener> listeners) {
for (ITestNGListener listener : listeners) {
//Lets filter out only IInvokedMethodListener instances.
if (listener instanceof IInvokedMethodListener) {
((IInvokedMethodListener) listener).beforeInvocation(iInvokedMethod, iTestResult);
}
}
}
protected void queueAfterInvocationListener(IInvokedMethod iInvokedMethod,
ITestResult iTestResult,
List<ITestNGListener> listeners) {
for (ITestNGListener listener : listeners) {
//Lets filter out only IInvokedMethodListener instances.
if (listener instanceof IInvokedMethodListener) {
((IInvokedMethodListener) listener).afterInvocation(iInvokedMethod, iTestResult);
}
}
}
private static ITestNGListener instantiate(String className) {
if (className == null || className.trim().isEmpty()) {
throw new IllegalArgumentException("Please provide a valid class name");
}
try {
Class<?> clazz = Class.forName(className);
if (!ITestNGListener.class.isAssignableFrom(clazz)) {
throw new IllegalArgumentException(className
+ " does not implement a TestNG listener");
}
return (ITestNGListener) clazz.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new TestNGException(e);
}
}
public static void main(String[] args) {
ITestNGListener tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { CatalogYamlTemplateTest.class });
testng.addListener(tla);
testng.run();
}
protected boolean hasMockitoTestNGListener(Class<?> clazz) {
Listeners listeners = clazz.getAnnotation(Listeners.class);
if (listeners == null) {
return false;
}
for (Class<? extends ITestNGListener> listenerClass : listeners.value()) {
if (listenerClass() == listenerClass) {
return true;
}
}
return false;
}
protected boolean hasMockitoTestNGListener(Class<?> clazz) {
Listeners listeners = clazz.getAnnotation(Listeners.class);
if (listeners == null) {
return false;
}
for (Class<? extends ITestNGListener> listenerClass : listeners.value()) {
if (listenerClass() == listenerClass) {
return true;
}
}
return false;
}
private boolean hasEverrestJettyListener(Class<?> clazz) {
Listeners listeners = clazz.getAnnotation(Listeners.class);
if (listeners == null) {
return false;
}
for (Class<? extends ITestNGListener> listenerClass : listeners.value()) {
if (EverrestJetty.class.isAssignableFrom(listenerClass)) {
return true;
}
}
return false;
}
@Test
public void runAllTckTests() throws Throwable {
TestNG testng = new TestNG();
ObjectFactoryImpl delegate = new ObjectFactoryImpl();
testng.setObjectFactory((IObjectFactory) (constructor, params) -> {
if (constructor.getDeclaringClass().equals(ReactiveStreamsCdiTck.class)) {
return tck;
}
else {
return delegate.newInstance(constructor, params);
}
});
testng.setUseDefaultListeners(false);
ResultListener resultListener = new ResultListener();
testng.addListener((ITestNGListener) resultListener);
testng.setTestClasses(new Class[]{ ReactiveStreamsCdiTck.class });
testng.setMethodInterceptor(new IMethodInterceptor() {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
methods.sort(Comparator.comparing(m -> m.getInstance().getClass().getName()));
return methods;
}
});
testng.run();
int total = resultListener.success.get() + resultListener.failed.get() + resultListener.skipped.get();
System.out.println(String.format("Ran %d tests, %d passed, %d failed, %d skipped.", total, resultListener.success.get(),
resultListener.failed.get(), resultListener.skipped.get()));
System.out.println("Failed tests:");
resultListener.failures.forEach(result -> {
System.out.println(result.getInstance().getClass().getName() + "." + result.getMethod().getMethodName());
});
if (resultListener.failed.get() > 0) {
if (resultListener.lastFailure.get() != null) {
throw resultListener.lastFailure.get();
}
else {
throw new Exception("Tests failed with no exception");
}
}
}
@Test
public void shouldLoadDataProviderTransformerService() {
var serviceImplementations = ServiceLoaderUtils.load(ITestNGListener.class, this.getClass().getClassLoader());
assertThat(serviceImplementations).hasSize(1);
assertThat(serviceImplementations.get(0)).isInstanceOf(DataProviderTransformer.class);
}