Can we exclude few tests from @BeforeClass?

1,372 views
Skip to first unread message

Jagriti Jain

unread,
Apr 24, 2019, 4:53:05 AM4/24/19
to testng-users
Hi,

I have a


   @BeforeClass
   public void setup(){
   
    //create restaurant id (say)

   }

  @Test(description="test 1")
   void restaurantIdDeleted(){
   .....
   } 


   @Test(description="test 2")
    void restaurantNameChanged(){
    ------
    }

  
   @Test(description="test 3")
    void menuItemChanged(){
   ------
   }
.......

The Before Class runs once and then the individual tests run 1 by 1.
Now, I have test1 which actually deletes the restuarantId which was generated in @BeforeClass, and test2 and test3 will fail now.
So, I thought of using @BeforeMethod instead of @BeforeClass, but then if I have 20 methods, then I am literally creating 20 restaurantIds, which is undesirable. I want a way to exclude 2-3 methods in which I am performing deletion or deactivation of restaurantId using @BeforeClass.


Please let me know if this is possible. 

Krishnan Mahadevan

unread,
Apr 24, 2019, 5:26:51 AM4/24/19
to testng...@googlegroups.com

Jagriti,

 

You can do this very easily by following the below steps:

 

  1. Define a marker interface which indicates that setup has to be skipped for a particular test.
  2. Annotate all test methods for which setup is to be skipped, using the interface defined in (1).
  3. Within your @BeforeMethod inspect the incoming “ITestResult” object and retrieve the method’s annotation to check if the marker interface defined in (1) was used to annotate the method. If the annotation was present, skip running the beforeMethod, else execute it.

 

The below sample shows this in action.

 

import static java.lang.annotation.ElementType.METHOD;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SelectiveConfigurationExecutionSample {

 
@BeforeMethod
 
public void beforeMethod(ITestResult result) {
    ITestNGMethod method = result.getMethod();
   
SkipSetup skipSetup =
        method.getConstructorOrMethod().getMethod().getAnnotation(
SkipSetup.class);
   
if (skipSetup == null) {
      System.
err.println("Running  setup for the method " + method.getMethodName() + "()");
    }
else {
      System.
err.println("Skipping setup for the method " + method.getMethodName() + "()");
    }
  }

 
@Test
 
public void a() {}

 
@Test
 
public void b() {}

 
@Test
  @SkipSetup
 
public void c() {}

 
@Test
 
public void d() {}

 
@Test
 
public void e() {}

 
@Test
  @SkipSetup
 
public void f() {}

 
@Test
 
public void g() {}

 
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
 
@Target({METHOD})
  @
interface SkipSetup {}
}

 

 

Running  setup for the method a()

 

Running  setup for the method b()

Skipping setup for the method c()

 

Running  setup for the method d()

Running  setup for the method e()

 

Skipping setup for the method f()

Running  setup for the method g()

 

===============================================

Default Suite

Total tests run: 7, Passes: 7, Failures: 0, Skips: 0

===============================================

 

 

Process finished with exit code 0

 

 

 

 

 

 

 

 

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.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 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.

Jagriti Jain

unread,
Apr 25, 2019, 2:37:23 AM4/25/19
to testng-users
Thanks for answering Krishnan, however I didn't get:

 SkipSetup skipSetup =
        method.getConstructorOrMethod().getMethod().getAnnotation(
SkipSetup.class);

you created a separate class elsewhere called Skipsetup, and then you created an object of this class called skipsetup, what exactly was there in the Skipsetup class, this might be silly, but I am a newbie.
Also there are Target and Retention annotation being used, I have never used them earlier.

Jagriti Jain

unread,
Apr 25, 2019, 5:11:07 AM4/25/19
to testng-users
attaching screenshot from my code, can't find Skipsetup
Screen Shot 2019-04-25 at 2.40.30 PM.png

Krishnan Mahadevan

unread,
Apr 25, 2019, 1:06:23 PM4/25/19
to testng...@googlegroups.com

Did you create a new class with the custom annotation SkipSetup ?

 

In the sample that I shared, I included its definition as a nested class. But you can have it reside in a separate file as well.

 

SkipSetup is a marker annotation.

To learn more about annotations in general refer https://dzone.com/articles/creating-custom-annotations-in-java

 

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.com/

 

From: <testng...@googlegroups.com> on behalf of Jagriti Jain <jagri...@gmail.com>


Reply-To: <testng...@googlegroups.com>
Date: Thursday, April 25, 2019 at 2:41 PM
To: testng-users <testng...@googlegroups.com>

--

Reply all
Reply to author
Forward
0 new messages