How do you get the results (pass, fail, skip count) programatically

534 views
Skip to first unread message

Bruce Ritter

unread,
Jan 24, 2022, 9:39:01 PM1/24/22
to testng-users
I am running programmatically an xml test file that I pass in, and would like to get the result counts programmatically after the run. Is there a way to do that?

                TestNG testng = new TestNG();
                List<String> testFilesList = new ArrayList<String>();
                 testFilesList.add(testFile);
                testng.setTestSuites(testFilesList);
                testng.run();
               int result = testng.getStatus();
               int pass = testng...?

I realize that I might have to do a little more than a single line call. The same question was asked in 2010, but the linked answer is no longer available (404 page error).

⇜Krishnan Mahadevan⇝

unread,
Jan 25, 2022, 12:12:26 AM1/25/22
to testng-users
Bruce, 

You are almost there. You just need to do the following:

1. Implement the TestNG interface org.testng.ITestListener (this is a TestNG listener that gets invoked for passed|failed|skipped tests)
2. Within this listener add logic such that it starts collecting the passed|failed|skipped tests and exposes them via getters.
3. Within your code, you instantiate this listener and wire it into TestNG via testng.addListener()
4. Now you can invoke testng.run() and then query the listener class for passed|failed|skipped tests


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/1afcda71-bbd1-4b13-81fc-90df7a74e58cn%40googlegroups.com.

Baubak

unread,
Jan 25, 2022, 7:02:48 AM1/25/22
to testng-users
Hi,

If I may, another way would be to implement a Test Listener Adapter in your test scenario itself. In your code, this is what I would do:

               TestNG testng = new TestNG();
               TestListenerAdapter tla = TestTools.fetchTestResultsHandler(testng);
                List<String> testFilesList = new ArrayList<String>();
                 testFilesList.add(testFile);
                testng.setTestSuites(testFilesList);
                testng.run();

and to access the results:

                 tla.getPassedTests()

At least this is how I do it.

Good luck

Best regards,

Baubak

Baubak

unread,
Jan 25, 2022, 6:45:39 PM1/25/22
to testng-users
Sorry forgot to add my code for fetching the adapter:

    /**
     * This method returns the test result listener attached to the testng
     * instance
     *
     * @param myTestNG
     *
     * @return a TestListenerAdapter that listens on the test results
     */
    public static TestListenerAdapter fetchTestResultsHandler(TestNG myTestNG) {
        List<ITestListener> testLisenerAdapaters = myTestNG.getTestListeners();

        if (testLisenerAdapaters.size() != 1)
            throw new IllegalStateException("We did not expect to have more than one adapter");

        return (TestListenerAdapter) testLisenerAdapaters.get(0);
    }
Reply all
Reply to author
Forward
0 new messages