Maven+Surefire+Testng 的顺序 - BeforeClass、BeforeTest、Test、Test、AfterTest、AfterClass

发布于 2024-10-04 18:31:21 字数 1310 浏览 7 评论 0原文

我期望得到以下输出:

Running TestSuite
[DEBUG] beforeClass
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] afterClass

但相反,这实际上发生了。注意 2 个问题:BeforeClass after BeforeTest 运行。其次,Before/AfterTest 运行一次

Running TestSuite
[DEBUG] beforeTest
[DEBUG] beforeClass
[DEBUG] test
[DEBUG] test
[DEBUG] afterClass
[DEBUG] afterTest

这是代码。

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNgAnnoTest {
   @BeforeClass
   public void beforeClass(){
      System.out.println("beforeClass");
   }
   @BeforeTest
   public void beforeTest(){
      System.out.println("beforeTest");
   }
   @Test
   public void test1(){
      System.out.println("test");
   }
   @Test
   public void test2(){
      System.out.println("test");
   }
   @AfterTest
   public void afterTest(){
      System.out.println("afterTest");

   }
   @AfterClass
   public void afterClass(){
      System.out.println("afterClass");
   }
}

对于任何对工具版本感兴趣的人: java macosx/1.6.0_22,mvn:2.2.1,surefire 2.6,testng 5.14.2

I expected the following output:

Running TestSuite
[DEBUG] beforeClass
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] afterClass

But instead, this actually happens. Notice 2 problems: BeforeClass runs after BeforeTest. Second, Before/AfterTest runs once.

Running TestSuite
[DEBUG] beforeTest
[DEBUG] beforeClass
[DEBUG] test
[DEBUG] test
[DEBUG] afterClass
[DEBUG] afterTest

Here's the code.

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNgAnnoTest {
   @BeforeClass
   public void beforeClass(){
      System.out.println("beforeClass");
   }
   @BeforeTest
   public void beforeTest(){
      System.out.println("beforeTest");
   }
   @Test
   public void test1(){
      System.out.println("test");
   }
   @Test
   public void test2(){
      System.out.println("test");
   }
   @AfterTest
   public void afterTest(){
      System.out.println("afterTest");

   }
   @AfterClass
   public void afterClass(){
      System.out.println("afterClass");
   }
}

For anyone curious about tool versions:
java macosx/1.6.0_22, mvn: 2.2.1, surefire 2.6, testng 5.14.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-10-11 18:31:21

愚蠢的我。将 After/BeforeTest 与 After/BeforeMethod 混淆。

[DEBUG] beforeClass
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] afterClass

替换 After/BeforeTest 会产生正确的执行顺序。

    @BeforeClass
    public static void beforeClass(){
        log.debug("beforeClass");
    }

    @BeforeMethod
    public void beforeMethod(){
        log.debug("beforeMethod");
    }

    @Test
    public void test1(){
        log.debug("test");
    }

    @Test
    public void test2(){
        log.debug("test");
    }

    @AfterMethod
    public void afterMethod(){
        log.debug("afterMethod");

    }

    @AfterClass
    public void afterClass(){
        log.debug("afterClass");
    }

Stupid me. Confused After/BeforeTest with After/BeforeMethod.

[DEBUG] beforeClass
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] afterClass

Replacing After/BeforeTest produced the correct exec ordering.

    @BeforeClass
    public static void beforeClass(){
        log.debug("beforeClass");
    }

    @BeforeMethod
    public void beforeMethod(){
        log.debug("beforeMethod");
    }

    @Test
    public void test1(){
        log.debug("test");
    }

    @Test
    public void test2(){
        log.debug("test");
    }

    @AfterMethod
    public void afterMethod(){
        log.debug("afterMethod");

    }

    @AfterClass
    public void afterClass(){
        log.debug("afterClass");
    }
愿得七秒忆 2024-10-11 18:31:21

正确的。 @BeforeTest/@AfterTest 包装标签,而不是测试方法。

Correct. @BeforeTest/@AfterTest wrap a tag, not a test method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文