user3024760
user3024760

Reputation: 176

TestNG not executing @BeforeMethod

I would like to use the same instance of @BeforeMethod for my tests from different classes but It just wont work

 package com.code.theCode

 public class theConfiguration{

    @BeforeMethod(groups = {"example"}, alwaysRun = true)
    public void setupMethod(){
       System.out.println("inside setupMethod");
    }
 }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 package com.code.test

 public class theTest{

    @Test(groups = {"example"}, alwaysRun = true)
    public void setupMethod(){
       System.out.println("inside test");
    }
 }

testng.xml

<suite name="AllTests" verbose="1">
   <test name="AllTests">
     <groups>
       <run>
          <include name="example">
       </run>
      </groups>
      <packages>
         <package name="com.code.*" />
      </packages>
   </test>

When run my tests I get blank sys outs

Any help greatly appreciated

Upvotes: 1

Views: 4766

Answers (1)

peetya
peetya

Reputation: 3628

Create an abstract class which include your configuration methods (what you want to use for more @Tests). After that extend your Test class with the created Abstract class. For example:

public abstract class configurationClass {

  @BeforeMethod
  public void beforeMethod() {
    System.out.println("beforeMethod");
  }

}

public class testClass extends configurationClass {

  @Test
  public void test1() {
    System.out.println("test1");
  }

  @Test
  public void test2() {
    System.out.println("test2");
  }

}

When you run the test class, the output will be:

beforeMethod
test1
beforeMethod
test2

Upvotes: 3

Related Questions