Skip to content

Commit

Permalink
Documentation for IExecutionListener
Browse files Browse the repository at this point in the history
Closes #56
  • Loading branch information
krmahadevan committed Jan 19, 2024
1 parent 02431c5 commit 8722e0b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/main/asciidoc/docs/method_invocations.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
=== Listening to TestNG lifecycle events

The listener {javadocs-base-url}/org/testng/IExecutionListener.html[IExecutionListener] allows you to be notified whenever TestNG is about to commence/conclude its execution. This listener can be used to perform setup and teardown activities at the test application layer itself ( for e.g., you could leverage this listener to boot up docker containers that are required for your application and shutdown them gracefully.)

Please note that this is the first listener TestNG would execute before it commences executing any of the `<suite>` found and also this would be the last listener to be invoked by TestNG (after the reporting listeners) before TestNG exits.

This listener should be declared, as explained in the section about xref:testng_listeners.adoc[TestNG listeners].

Here's a sample listener implementation.

[source, java]

----
import org.testng.IExecutionListener;
public class SimpleExecutionListener implements IExecutionListener {
@Override
public void onExecutionStart() {
System.err.println("TestNG is commencing execution");
}
@Override
public void onExecutionFinish() {
System.err.println("TestNG is finished execution");
}
}
----

Here's a sample test class that uses this above listener.

[source, java]

----
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(SimpleExecutionListener.class)
public class SampleTestCase {
@Test
public void testMethod1() {}
}
----

The execution output would look like below:

[source, bash]

----
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
TestNG is commencing execution
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
TestNG is finished execution
Process finished with exit code 0
----


=== Listening to method invocations

The listener {javadocs-base-url}/org/testng/IInvokedMethodListener.html[IInvokedMethodListener] allows you to be notified whenever TestNG is about to invoke a test (annotated with `@Test`) or configuration (annotated with any of the `@Before` or `@After` annotation) method and declare it as a listener, as explained in the section about xref:testng_listeners.adoc[TestNG listeners].
Expand Down
2 changes: 1 addition & 1 deletion src/main/asciidoc/docs/testng_listeners.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ There are several interfaces that allow you to modify TestNG's behavior. These i
* IConfigurationListener(xref:method_invocations.adoc#_listening_to_configuration_invocations[doc], {javadocs-base-url}/org/testng/IConfigurationListener.html[javadoc])
* IDataProviderInterceptor(doc, {javadocs-base-url}/org/testng/IDataProviderInterceptor.html[javadoc])
* IDataProviderListener(xref:method_invocations.adoc#_listening_to_data_provider_invocations[doc], {javadocs-base-url}/org/testng/IDataProviderListener.html[javadoc])
* IExecutionListener(doc, {javadocs-base-url}/org/testng/IExecutionListener.html[javadoc])
* IExecutionListener(xref:method_invocations.adoc#_listening_to_testng_lifecycle_events[doc], {javadocs-base-url}/org/testng/IExecutionListener.html[javadoc])
* IExecutionVisualiser(doc, {javadocs-base-url}/org/testng/IExecutionVisualiser.html[javadoc])
* IHookable (xref:ihookable.adoc[doc], {javadocs-base-url}/org/testng/IHookable.html[javadoc])
* IConfigurable(xref:iconfigurable.adoc[doc], {javadocs-base-url}/org/testng/IConfigurable.html[javadoc])
Expand Down

0 comments on commit 8722e0b

Please sign in to comment.