TestNG 不执行 @BeforeMethod [英] TestNG not executing @BeforeMethod

查看:57
本文介绍了TestNG 不执行 @BeforeMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对来自不同类的测试使用相同的 @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

非常感谢任何帮助

推荐答案

创建一个抽象类,其中包含您的配置方法(您想用于更多 @Tests 的方法).之后,使用创建的抽象类扩展您的测试类.例如:

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

这篇关于TestNG 不执行 @BeforeMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆