TestNG 没有执行@BeforeMethod

TestNG not executing @BeforeMethod

我想对来自不同 类 的测试使用相同的 @BeforeMethod 实例,但它不起作用

 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>

当 运行 我的测试时,我得到空白的系统输出

非常感谢任何帮助

创建一个摘要class,其中包括您的配置方法(您希望用于更多@Tests 的方法)。之后,使用创建的摘要 class 扩展您的测试 class。例如:

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");
  }

}

当您 运行 测试 class 时,输出将是:

beforeMethod
test1
beforeMethod
test2