【问题标题】:TestNG not executing @BeforeMethodTestNG 不执行@BeforeMethod
【发布时间】:2015-07-18 06:53:12
【问题描述】:

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

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

非常感谢任何帮助

【问题讨论】:

  • 如何执行?
  • 如果你得到空白的系统输出,那么你的BeforeMethod 以及你的测试没有被执行,所以问题不仅仅是BeforeMethod。你是如何运行测试的? IDE?安慰?有哪些论据?
  • 是的,通过并打印 System.out.println("inside test");但不会打印@BeforeTest syso ...它不会在日志中显示错误:(
  • 你是如何运行测试的? IDE?安慰?有哪些论据?
  • Eclipse IDE,TestNG 运行配置指向 testng.xml 套件文件

标签: java testng


【解决方案1】:

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

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

}

当你运行测试类时,输出将是:

beforeMethod
test1
beforeMethod
test2

【讨论】:

  • 嗨,peetya,我已经做到了,但我不想使用继承,因此我认为 xml 方法会更好。
  • 由于某种原因,它会在配置类中调用 BeforeSuite 而不是 BeforeMethod....BeforeMethod 应该在 (AT-SIGN)Test 运行之前调用,对吗?
  • 我不确定你只能从 xml 中实现它。是的,beforeMethod 在@Tests 之前运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 2020-12-30
相关资源
最近更新 更多