栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

TestNg不同测试类中同名的groups引发的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TestNg不同测试类中同名的groups引发的问题

TestNg不同测试类中同名的groups引发的问题

原来有这么一个测试类,其中groups有one和two

package com.newcrud.testngTest;

import org.testng.annotations.*;

public class TestOne {
    @Test
    public void testOne(){
        System.out.println("testOne");
    }
    @Test
    public void testTwo(){
        System.out.println("testTwo");
    }
    @Test(groups = "one")
    public void testThree(){
        System.out.println("testThree");
    }
    @Test(groups = "one")
    public void testFour(){
        System.out.println("testFour");
    }
    @Test(groups = "two")
    public void testFive(){
        System.out.println("testFive");
    }
    @Test(groups = "two")
    public void testSix(){
        System.out.println("testSix");
    }
    @BeforeSuite
    public void testSeven(){
        System.out.println("@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行");
    }
    @AfterSuite
    public void testEight(){
        System.out.println("@AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。");
    }
    @BeforeClass
    public void testNine(){
        System.out.println("@BeforeClass,在当前类的第一个测试方法之前运行。");
    }
    @AfterClass
    public void testTen(){
        System.out.println("@AfterClass,运行当前类中的所有测试方法之后都运行。");
    }
    @BeforeGroups(groups = {"one","two"})
    public void testEleven(){
        System.out.println("@BeforeGroups:在调用属于该组的第一个测试方法之前运行.");
    }
    @AfterGroups(groups = {"one","two"})
    public void testTwelve(){
        System.out.println("@AfterGroups:在调用属于组的最后一个测试方法之后运行。");
    }
    @BeforeTest
    public void testThirteen(){
        System.out.println("@BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。");
    }
    @AfterTest
    public void testFourteen(){
        System.out.println("@AfterTest - 对于套件测试,在运行属于标签内的类的所有测试方法都已运行之后运行");
    }
    @BeforeMethod
    public void testFifteen(){
        System.out.println("@BeforeMethod - 在每个测试方法之前运行。");
    }
    @AfterMethod
    public void testSixteen(){
        System.out.println("@AfterMethod - 在每个测试方法之后运行。");
    }

}

然后新写了一个测试类,groups也是one和two

package com.newcrud.testngTest;

import org.testng.annotations.Test;

public class TestFour {
    @Test(groups = "one")
    public void testA()  {
        System.out.println("A");
    }
    @Test(groups = "two")
    public void testB()  {
        System.out.println("B");
    }
    @Test(groups = "one")
    public void testC()  {
        System.out.println("C");
    }
    @Test(groups = "two")
    public void testD()  {
        System.out.println("D");
    }
}

执行结果,发现明明属于第一个测试类的方法,执行到了第二个测试类的结果里

@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行
@BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。
@BeforeGroups:在调用属于该组的第一个测试方法之前运行.
A
@BeforeGroups:在调用属于该组的第一个测试方法之前运行.
B
C
@AfterGroups:在调用属于组的最后一个测试方法之后运行。
D
@AfterGroups:在调用属于组的最后一个测试方法之后运行。
@AfterTest - 对于套件测试,在运行属于标签内的类的所有测试方法都已运行之后运行
@AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。

我们再把第二个测试类的groups替换成不重复的

package com.newcrud.testngTest;

import org.testng.annotations.Test;

public class TestFour {
    @Test(groups = "ten")
    public void testA()  {
        System.out.println("A");
    }
    @Test(groups = "ten")
    public void testB()  {
        System.out.println("B");
    }
    @Test(groups = "eleven")
    public void testC()  {
        System.out.println("C");
    }
    @Test(groups = "eleven")
    public void testD()  {
        System.out.println("D");
    }
}

执行结果

A
B
C
D
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号