代码之家  ›  专栏  ›  技术社区  ›  Iain Samuel McLean Elder

intellijidea+TestNG:在组中的每个测试之前运行一个方法

  •  1
  • Iain Samuel McLean Elder  · 技术社区  · 14 年前

    我正在学习使用TestNG来实现IntelliJ IDEA 9。

    据我所知,在一个叫做 name 就是给它加注解 @Test(group = "name") . 要在每次测试前运行方法,请使用 @BeforeMethod

    在我的测试设置中,我希望在每个测试之前只在特定的组中运行一个方法。所以有一个方法 beforeA 在组中的每个测试之前运行 A ,一种方法 beforeB B

    示例代码:

    public class TestExample
    {
        @BeforeMethod(groups = "A")
        public void beforeA()
        {
            System.out.println("before A");
        }
    
        @BeforeMethod(groups = "B")
        public void beforeB()
        {
            System.out.println("before B");
        }
    
        @Test(groups = "A")
        public void A1()
        {
            System.out.println("test A1");
        }
    
        @Test(groups = "A")
        public void A2()
        {
            System.out.println("test A2");
        }
    
        @Test(groups = "B")
        public void B1()
        {
            System.out.println("test B1");
        }
    
        @Test(groups = "B")
        public void B2()
        {
            System.out.println("test B2");
        }
    }
    

    before A
    test A1
    before A
    test A2
    before B
    test B1
    before B
    test B2
    

    但我得到了以下信息:

    before A
    before B
    before A
    before B
    test A2
    before A
    before B
    before A
    before B
    test B1
    
    ===============================================
    
    test B2
    
    ===============================================
    Custom suite
    Total tests run: 4, Failures: 0, Skips: 0
    ===============================================
    

    我做错什么了?

    4 回复  |  直到 14 年前
        1
  •  19
  •   Colin Hebert    14 年前
    1. 列表不整齐,这是intelliJ的错。在命令行或maven中运行测试,顺序将是正确的。
    2. @BeforeMethod @AfterMethod 似乎与团体决裂了。
    3. IntelliJ记住您以前使用过的组,如果您使用的组尚未记住,则会显示消息“groupx is undefined”。只是压力 中高音 在一个未定义的组上记住它。

    资源:

        2
  •  3
  •   Ilya    14 年前

    http://youtrack.jetbrains.net/issue/IDEA-67653 我们得投赞成票,这样JetBrains就能搞定了

        3
  •  3
  •   TEH EMPRAH    12 年前

    @BeforeMethod(groups =...) 它将在类中的每个方法之前运行。不同的是,它只属于一个特定的群体,仅此而已。 See DOCS

        4
  •  1
  •   Sahar Rabinoviz    10 年前

    为了做到这一点,你必须配置你自己testng.xml文件正确地。对于预期的输出,应该是这样的

    <suite....>
     <test name="A">
      <groups>
        <run>
       <include name="A"/>
        </run>
      </groups>
      <classes>
       <class name="...TestExample"/>
      </classes>
     </test>
     <test name="B">
      <groups>
        <run>
       <include name="B"/>
        </run>
      </groups>
      <classes>
       <class name="...TestExample"/>
      </classes>
     </test>
    </suite>