Test Specific @AfterMethod

196 views
Skip to first unread message

Gaurang shah

unread,
Jul 7, 2016, 2:45:16 PM7/7/16
to testng-users
Hi Guys, 

I am writing a test case using TestNG and I have come across a very specific situation where I need to run teardown which does some extra work for this particular method(testcase) only. 

For example. 
My generic teardown, closed the browser only. However for this particular test case I need to undo the things I have done in my test so that other test passes. 

So far I am using the following solution. It's working however, it doesn't look elegant. Also if condition will be executed for each and every test case, so it's time-consuming as well.

Cucumber has an elegant way to handle this, something called tagged hooks, Can we implement something like this in TestNG? 

Look at the link below for tagged hooks.




@AfterMethod
public void changeStatusToNew(Method method){

    if (method.getName().equalsIgnoreCase("changeStatus")) {
// Do Test Case Specific thing


    }
//Call Generic Teardown
    super.tearDown();
}




Cédric Beust ♔

unread,
Jul 7, 2016, 3:14:56 PM7/7/16
to testng...@googlegroups.com
Gaurang,

Given how rare this kind of situation is, I think your approach is fine and not worth adding as a feature.


-- 
Cédric


--
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 post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Gaurang shah

unread,
Jul 7, 2016, 3:33:14 PM7/7/16
to testng-users, ced...@beust.com
Hi Cedric, 

Thanks for quick response. I am not asking that to add that as a feature. I would like to know if there is any way I can implement my own? I think it's possible with TesNG providing different listeners. 

And if someone has already done that, that will save a hell lot of time.

Cédric Beust ♔

unread,
Jul 7, 2016, 4:04:11 PM7/7/16
to Gaurang shah, testng-users

Possibly but I would do it exactly like you did. That’s why TestNG passes you the method in your @AfterMethod: so you can perform method specific actions should you need to.


-- 
Cédric

⇜Krishnan Mahadevan⇝

unread,
Jul 7, 2016, 11:24:10 PM7/7/16
to testng...@googlegroups.com
Cucumber has an elegant way to handle this, something called tagged hooks, Can we implement something like this in TestNG? 
Look at the link below for tagged hooks.
http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Gaurav,

I was always under the impression that the tagged mechanism of executing scenarios (or) tying scenarios to hooks was equal to one associating groups to @Test or @Before/@After methods no ?

So if you were to take that same approach then you would maybe do the same. 

If you would like to make your implementation look sophisticated [ i.e., you should have the luxury of being able to refactor your @Test annotated method's name and not have to worry about remembering to go back and check the if condition in your @After method], then you could do something like below :

  1. Create a custom annotation. Lets call it as @ExecuteCustomCleanup
  2. Within your @AfterMethod, you query the java.lang.Method object for the presence of the custom annotation.
  3. If custom annotation [ defined in (1) was found ] then do extra cleanup else skip it.
PS: This would still NOT do away with the need for having if conditions [ am not quite sure as to how costly would this if condition execution be to affect execution time ], but what this will do away is, to de-couple the hard-wired dependency of your @AfterMethod on a particular method's name in order to do something extra and lets you refactor methods at will without having to worry about breaking anything.


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 Scribbings @ http://rationaleemotions.wordpress.com/
Reply all
Reply to author
Forward
0 new messages