简体   繁体   English

继承期间TestNG中@BeforeClass方法的执行

[英]@BeforeClass method execution in TestNG during inheritence

I have just started using TestNG, and I had a question in mind.我刚刚开始使用TestNG,我有一个问题。 If class A has a @BeforeSuite method and class B is inheriting class A like in the below example:如果类A有一个@BeforeSuite方法并且类B继承类A ,如下例所示:

class A file: A类文件:

class A
{
  @BeforeSuite
  public void beforeTest()
  {
    //something
  }
}

class B file: B类文件:

class B extends A
{
 @Test
 public void executeTest()
 {
  //something
 }
}

So, in this scenario, the @BeforeSuite from class A will run for sure when I try and execute ClassB:executeTest() .因此,在这种情况下,当我尝试执行ClassB:executeTest()时, A类的@BeforeSuite肯定会运行。 Is there any way I could disable the @BeforeSuite method from running, without excluding the inheritance or disabling the @Test method?有什么方法可以禁用@BeforeSuite方法的运行,而不排除继承或禁用@Test方法?

You can override method like in example below.您可以像下面的示例一样覆盖方法。 @BeforeSuite of the base class will not be executed in this case.在这种情况下不会执行基类的@BeforeSuite

TestA:测试A:

import org.testng.annotations.BeforeSuite;
import org.testng.log4testng.Logger;

public class TestA {
    protected static Logger LOGGER = Logger.getLogger(TestA.class);

    @BeforeSuite
    public void beforeSuite() {
        LOGGER.warn("beforeSuite - TestA");
    }
}

TestB:测试B:

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class TestB extends TestA {

    @BeforeSuite
    public void beforeSuite() {
        LOGGER.warn("beforeSuite - TestB");
    }

    @Test
    public void executeTest() {
        LOGGER.warn("executeTest - TestB");
    }
}

Run TestB and check output:运行 TestB 并检查输出:

[TestA] [WARN] beforeSuite - TestB
[TestA] [WARN] executeTest - TestB

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM