Skip to main content

TestNG

The TestNG listener adds support to use the @Message annotation on your test classes and methods.

Registering the listener with TestNG​

In order to integrate Alerticorn with TestNG, the MessageListener must be registered as a TestNG Listener.

Using @Listeners annotation​

The simplest method is to use the @Listeners annotation from TestNG:

Example of registering the listener
@Listeners(MessageListener::class)
class MyTest {
//...
}

Automatic discovery via SPI​

The alerticorn-testng module ships with a META-INF/services/org.testng.ITestNGListener file, so TestNG will automatically discover and register the listener if the jar is on the classpath.

Via testng.xml​

You can also register the listener in your testng.xml configuration:

<suite name="My Suite">
<listeners>
<listener class-name="nl.vanwollingen.alerticorn.testng.MessageListener"/>
</listeners>
<!-- ... -->
</suite>

Supported events​

The TestNG listener supports the following events:

EventWhen it fires
Event.PASSTest passes (onTestSuccess)
Event.FAILTest fails (onTestFailure)
Event.EXCEPTIONTest fails with a throwable (fires alongside FAIL in onTestFailure)
Event.SKIPTest is skipped (onTestSkipped)
Event.ANYAny of the above events
Event.SUITE_STARTSuite starts (onStart)
Event.SUITE_COMPLETESuite finishes (onFinish)

Suite-level notifications​

You can receive notifications when a test suite starts or completes by placing @Message annotations with suite events on your test class:

@Message(title = "Test suite completed")
@Message.Events([Event.SUITE_COMPLETE])
@Message.Platform("slack")
@Message.Channel("ci-results")
class MyTest {

@Test
fun myTest() {
// ...
}
}

When the suite completes, the notification message will be enriched with suite summary details: suite (name), total, passed, failed, and skipped counts.

FAQ​

How does Event.EXCEPTION work in TestNG vs JUnit?​

In JUnit, Event.EXCEPTION fires before the test is marked as failed (via TestExecutionExceptionHandler), and Event.FAIL fires after. This means a test annotated with both events gets two notifications.

In TestNG, there is no pre-outcome exception hook. Both Event.EXCEPTION and Event.FAIL are checked in onTestFailure, and only one notification is sent. Event.EXCEPTION only triggers when a throwable is present on the test result.