org.testng.ITestNGListener Java Examples

The following examples show how to use org.testng.ITestNGListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassLevelDirtiesContextTestNGTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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);
}
 
Example #2
Source File: FailingBeforeAndAfterMethodsTestNGTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #3
Source File: ClassLevelDirtiesContextTestNGTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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);
}
 
Example #4
Source File: FailingBeforeAndAfterMethodsTestNGTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #5
Source File: ElectPrimaryTest.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
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));
    }
 
Example #6
Source File: Helpers.java    From AppiumTestDistribution with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: EverrestJetty.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: MockitoTestNGListener.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: MockitoTestNGListener.java    From mockito-cookbook with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: CatalogYamlTemplateTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #11
Source File: Helpers.java    From AppiumTestDistribution with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #12
Source File: Helpers.java    From AppiumTestDistribution with GNU General Public License v3.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #13
Source File: Helpers.java    From AppiumTestDistribution with GNU General Public License v3.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #14
Source File: TestNGRunner.java    From test-data-supplier with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: AllureTestNgTest.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #16
Source File: TestNgBase.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
}
 
Example #17
Source File: TestNGRunner.java    From agent with MIT License 5 votes vote down vote up
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;
}
 
Example #18
Source File: ReflectionUtilsTests.java    From test-data-supplier with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldLoadDataProviderTransformerService() {
    var serviceImplementations = ServiceLoaderUtils.load(ITestNGListener.class, this.getClass().getClassLoader());
    assertThat(serviceImplementations).hasSize(1);
    assertThat(serviceImplementations.get(0)).isInstanceOf(DataProviderTransformer.class);
}
 
Example #19
Source File: ReactiveStreamsArquillianTck.java    From microprofile-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
@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");
        }
    }
}