[testng-dev] How to implement @BeforeClass/@AfterClass programmatically

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[testng-dev] How to implement @BeforeClass/@AfterClass programmatically

Ivan Hrytsyuk
Hello everybody.

I am implementing TestNG support for custom annotation that could be applied for test class or test method.
If a class/method is annotated - my custom logic is invoked before and after the class/method.
In other words I'm implementing functionality similar to @BeforeClass/@AfterClass and @BeforeMethod/@AfterMethod (but only in case test method is annotated with my custom annotation)

My current implementation extends TestListenerAdapter (which implements both org.testng.ITestListener and org.testng.IConfigurationListener) and overrides next methods:
  • public void onStart(final ITestContext testContext)
  • public void onFinish(final ITestContext testContext)
  • methods to deal with method-level annotation: onTestStart, onTestSuccess, onTestFailure, onTestFailedButWithinSuccessPercentage
The biggest problem at the moment is running my custom logic before every test class.
I was very surprised when noticed that onStart(...) method from org.testng.ITestListener is invoked before every test configuration, not before every test class.

Real example:
  • Both com.ihrytsyuk.Class1Test and com.ihrytsyuk.Class3Test have my custom annotation at class-level
  • XML snippet from testng.xml:
    <test verbose="2" name="com.ihrytsyuk.PositiveTests" annotations="JDK">
            <class name="com.ihrytsyuk.Class1Test" />
            <class name="com.ihrytsyuk.Class2Test" />
            <class name="com.ihrytsyuk.Class3Test" />

Actual behavior:
  • My custom logic is invoked ONLY before and after com.ihrytsyuk.PositiveTests test configuration
Expected behavior:
  • My custom logic should be invoked before and after both com.ihrytsyuk.Class1Test and com.ihrytsyuk.Class3Test test classes
Can anyone help me to achieve the expected behavior?

- Thank you in advance, Ivan

You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en.