Java Code Examples for org.testng.TestNG#addListener()

The following examples show how to use org.testng.TestNG#addListener() . 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: SecureEmbeddedServerTestBase.java    From atlas with Apache License 2.0 7 votes vote down vote up
/**
 * Runs the existing webapp test cases, this time against the initiated secure server instance.
 * @throws Exception
 */
@Test
public void runOtherSuitesAgainstSecureServer() throws Exception {
    final PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CERT_STORES_CREDENTIAL_PROVIDER_PATH, providerUrl);
    // setup the credential provider
    setupCredentials();

    try {
        secureEmbeddedServer = new SecureEmbeddedServer(
            EmbeddedServer.ATLAS_DEFAULT_BIND_ADDRESS, securePort, TestUtils.getWarPath()) {
            @Override
            protected PropertiesConfiguration getConfiguration() {
                return configuration;
            }
        };
        secureEmbeddedServer.server.start();

        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[]{AdminJerseyResourceIT.class, EntityJerseyResourceIT.class,
                TypesJerseyResourceIT.class});
        testng.addListener(tla);
        testng.run();

    } finally {
        secureEmbeddedServer.server.stop();
    }

}
 
Example 2
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 3
Source File: TestNgExecutor.java    From JGiven with Apache License 2.0 6 votes vote down vote up
@Override
public TestExecutionResult execute( Class<?> testClass, String testMethod ) {
    TestNgExecutionResult result = new TestNgExecutionResult();
    ScenarioTestListenerAdapter testListenerAdapter = new ScenarioTestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses( new Class<?>[] { testClass } );
    if( testMethod != null ) {
        methodName = testMethod;
        testng.addMethodSelector( MethodSelector.class.getName(), 10 );
    }
    testng.addListener( testListenerAdapter );
    Config.config().setReportEnabled( false );
    testng.run();
    Config.config().setReportEnabled( true );
    result.reportModel = testListenerAdapter.reportModel;
    result.testResults = testListenerAdapter.testResults;
    return result;
}
 
Example 4
Source File: SecureEmbeddedServerTestBase.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the existing webapp test cases, this time against the initiated secure server instance.
 * @throws Exception
 */
@Test
public void runOtherSuitesAgainstSecureServer() throws Exception {
    final PropertiesConfiguration configuration = new PropertiesConfiguration();
    configuration.setProperty(CERT_STORES_CREDENTIAL_PROVIDER_PATH, providerUrl);
    // setup the credential provider
    setupCredentials();

    try {
        secureEmbeddedServer = new SecureEmbeddedServer(securePort, TestUtils.getWarPath()) {
            @Override
            protected PropertiesConfiguration getConfiguration() {
                return configuration;
            }
        };
        secureEmbeddedServer.server.start();

        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[]{AdminJerseyResourceIT.class, EntityJerseyResourceIT.class,
                MetadataDiscoveryJerseyResourceIT.class,
                TypesJerseyResourceIT.class});
        testng.addListener(tla);
        testng.run();

    } finally {
        secureEmbeddedServer.server.stop();
    }

}
 
Example 5
Source File: ClassLevelDirtiesContextTestNGTests.java    From spring4-understanding with Apache License 2.0 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(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 6
Source File: FailingBeforeAndAfterMethodsTestNGTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void runTestAndAssertCounters() throws Exception {
	final TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
	final TestNG testNG = new TestNG();
	testNG.addListener(listener);
	testNG.setTestClasses(new Class<?>[] { this.clazz });
	testNG.setVerbose(0);
	testNG.run();

	assertEquals("Verifying number of test starts for test class [" + this.clazz + "].",
		this.expectedTestStartCount, listener.testStartCount);
	assertEquals("Verifying number of successful tests for test class [" + this.clazz + "].",
		this.expectedTestSuccessCount, listener.testSuccessCount);
	assertEquals("Verifying number of failures for test class [" + this.clazz + "].", this.expectedFailureCount,
		listener.testFailureCount);
	assertEquals("Verifying number of failed configurations for test class [" + this.clazz + "].",
		this.expectedFailedConfigurationsCount, listener.failedConfigurationsCount);
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
Source File: TestNGMainClass.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	TestNG testSuite = new TestNG();
	testSuite.setTestClasses(new Class[] { Test5.class });
	testSuite.addListener(new Test5SuiteListener());
	testSuite.setDefaultSuiteName("My Test Suite");
	testSuite.setDefaultTestName("My Test");
	testSuite.setOutputDirectory("/Users/pankaj/temp/testng-output");
	testSuite.run();
}
 
Example 13
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 14
Source File: TestNGShouldFailWhenMockitoListenerFailsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private TestNG new_TestNG_with_failure_recorder_for(Class<?>... testNGClasses) {
    TestNG testNG = new TestNG();
    testNG.setVerbose(0);
    testNG.setUseDefaultListeners(false);
    testNG.addListener(failureRecorder);

    testNG.setTestClasses(testNGClasses);
    return testNG;
}
 
Example 15
Source File: TestNGTests.java    From quickperf with Apache License 2.0 5 votes vote down vote up
public static TestNGTests createInstance(Class<?> testClass) {
    TestNG testNG = new TestNG();
    testNG.setUseDefaultListeners(false);
    testNG.setVerbose(0);
    Class[] testClasses = {testClass};
    testNG.setTestClasses(testClasses);

    TestListenerAdapter testListenerAdapter = new TestListenerAdapter();
    testNG.addListener(testListenerAdapter);

    return new TestNGTests(testNG, testListenerAdapter);
}
 
Example 16
Source File: TestNGRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Throwable {
  for (String className : testClassNames) {

    Class<?> testClass = Class.forName(className);

    List<TestResult> results;
    if (!mightBeATestClass(testClass)) {
      results = Collections.emptyList();
    } else {
      results = new ArrayList<>();
      TestNG testng = new TestNG();
      testng.setUseDefaultListeners(false);
      testng.addListener(new FilteringAnnotationTransformer(results));
      testng.setTestClasses(new Class<?>[] {testClass});
      testng.addListener(new TestListener(results));
      // use default TestNG reporters ...
      testng.addListener(new SuiteHTMLReporter());
      testng.addListener((IReporter) new FailedReporter());
      testng.addListener(new XMLReporter());
      testng.addListener(new EmailableReporter());
      // ... except this replaces JUnitReportReporter ...
      testng.addListener(new JUnitReportReporterWithMethodParameters());
      // ... and we can't access TestNG verbosity, so we remove VerboseReporter
      testng.run();
    }

    writeResult(className, results);
  }
}
 
Example 17
Source File: TestngRun.java    From Leo with Apache License 2.0 5 votes vote down vote up
public  TestngRun() {
	tng=new TestNG();
	listener=new TestListenerAdapter();//定义监听器类型
	tng.addListener(listener);
	xmlFileList=new ArrayList<>();//记录测试使用的xml文件路径列表
	testReport=new TestReport();//记录测试报告测试报告信息
	runInfo=new TestRunInfo();
}
 
Example 18
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");
        }
    }
}
 
Example 19
Source File: CarinaListener.java    From carina with Apache License 2.0 4 votes vote down vote up
private void checkHealth(ISuite suite, String className, String[] methods) {

        if (className.isEmpty()) {
            return;
        }

        // create runtime XML suite for health check
        XmlSuite xmlSuite = new XmlSuite();
        xmlSuite.setName("HealthCheck XmlSuite - " + className);

        XmlTest xmlTest = new XmlTest(xmlSuite);
        xmlTest.setName("HealthCheck TestCase");
        XmlClass xmlHealthCheckClass = new XmlClass();
        xmlHealthCheckClass.setName(className);

        // TestNG do not execute missed methods so we have to calulate expected
        // methods count to handle potential mistakes in methods naming
        int expectedMethodsCount = -1;
        if (methods != null) {
            // declare particular methods if they are provided
            List<XmlInclude> methodsToRun = constructIncludes(methods);
            expectedMethodsCount = methodsToRun.size();
            xmlHealthCheckClass.setIncludedMethods(methodsToRun);
        }

        xmlTest.setXmlClasses(Arrays.asList(new XmlClass[] { xmlHealthCheckClass }));
        xmlSuite.setTests(Arrays.asList(new XmlTest[] { xmlTest }));

        LOGGER.info("HealthCheck '" + className + "' is started.");
        LOGGER.debug("HealthCheck suite content:" + xmlSuite.toXml());

        // Second TestNG process to run HealthCheck
        TestNG testng = new TestNG();
        testng.setXmlSuites(Arrays.asList(xmlSuite));

        TestListenerAdapter tla = new TestListenerAdapter();
        testng.addListener(tla);

        testng.run();
        synchronized (this) {
            boolean passed = false;
            if (expectedMethodsCount == -1) {
                if (tla.getPassedTests().size() > 0 && tla.getFailedTests().size() == 0
                        && tla.getSkippedTests().size() == 0) {
                    passed = true;
                }
            } else {
                LOGGER.info("Expected passed tests count: " + expectedMethodsCount);
                if (tla.getPassedTests().size() == expectedMethodsCount && tla.getFailedTests().size() == 0
                        && tla.getSkippedTests().size() == 0) {
                    passed = true;
                }
            }
            if (passed) {
                LOGGER.info("HealthCheck suite '" + className + "' is finished successfully.");
            } else {
                throw new SkipException("Skip test(s) due to health check failures for '" + className + "'");
            }
        }
    }
 
Example 20
Source File: HybridClusterIntegrationTestCommandLineRunner.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
    throws Exception {
  int numArgs = args.length;
  if (!((numArgs == 6) || (numArgs == 7 && args[0].equals("--llc")))) {
    printUsage();
  }

  CustomHybridClusterIntegrationTest._enabled = true;

  int argIdx = 0;
  if (args[0].equals("--llc")) {
    CustomHybridClusterIntegrationTest._useLlc = true;
    argIdx++;
  }

  CustomHybridClusterIntegrationTest._tableName = args[argIdx++];
  File schemaFile = new File(args[argIdx++]);
  CustomHybridClusterIntegrationTest._schema = Schema.fromFile(schemaFile);
  String timeColumnName = args[argIdx++];
  CustomHybridClusterIntegrationTest._timeColumnName =
      (CustomHybridClusterIntegrationTest._schema.getFieldSpecFor(timeColumnName) != null) ? timeColumnName : null;
  File dataDir = new File(args[argIdx++]);
  Preconditions.checkState(dataDir.isDirectory());
  CustomHybridClusterIntegrationTest._dataDir = dataDir;
  CustomHybridClusterIntegrationTest._invertedIndexColumns = Arrays.asList(args[argIdx++].split(","));
  CustomHybridClusterIntegrationTest._sortedColumn = args[argIdx];

  TestListenerAdapter testListenerAdapter = new TestListenerAdapter();
  TestNG testNG = new TestNG();
  testNG.setTestClasses(new Class[]{CustomHybridClusterIntegrationTest.class});
  testNG.addListener(testListenerAdapter);
  testNG.run();

  System.out.println(testListenerAdapter.toString());
  boolean success = true;
  List<ITestResult> skippedTests = testListenerAdapter.getSkippedTests();
  if (!skippedTests.isEmpty()) {
    System.out.println("Skipped tests: " + skippedTests);
    for (ITestResult skippedTest : skippedTests) {
      System.out.println(skippedTest.getName() + ": " + skippedTest.getThrowable());
    }
    success = false;
  }
  List<ITestResult> failedTests = testListenerAdapter.getFailedTests();
  if (!failedTests.isEmpty()) {
    System.err.println("Failed tests: " + failedTests);
    for (ITestResult failedTest : failedTests) {
      System.out.println(failedTest.getName() + ": " + failedTest.getThrowable());
    }
    success = false;
  }
  if (success) {
    System.exit(0);
  } else {
    System.exit(1);
  }
}