java - @BeforeMethod/@AfterMethod(onlyForGroups)方法没有执行,如果执行属于该组的测试方法

标签 java selenium selenium-webdriver testng

我正在运行一个测试套件,其中的测试方法属于特定组。

下面是 Selenium 代码:​​

public class BaseClass
{

    @BeforeMethod(onlyForGroups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }

}

public class TC_003 extends BaseClass
{

    @Test(groups = {"P1"})
    public void tCase6()
    {
        System.out.println("Inside testcase 6");
    }

    @Test(groups = {"P2"})
    public void tCase7()
    {
        System.out.println("Inside testcase 7");
    }

    @Test(groups = {"P3"})
    public void tCase8()
    {
        System.out.println("Inside testcase 8");
    }

}

下面是 testng.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
    <groups>
        <run>
            <include name=".*"/>
        </run>
    </groups>
    <classes>
        <class name="testing.TC_003"/>
    </classes>
</test>
</suite>

实际输出:

Inside testcase 6
Inside testcase 7
Inside testcase 8

预期输出:

Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called

测试方法执行了,但是@BeforeMethod/@AfterMethod没有执行。仅当我们在 testng.xml 文件中包含某些组时才会出现此问题。但是如果我们排除某些组或者在 testng.xml 文件中不使用 groups 标签,那么它们就会被执行。

按照建议here ,当前的解决方法是使用alwaysRun=true 标志和onlyForGroups 标志。但是,如果我们应用此解决方法,并且如果前面/父配置方法中存在任何 SkipException,则即使要跳过测试方法,也会强制执行 @BeforeMethod/@AfterMethod 方法。记录了类似的问题here ,当前面/父配置方法失败时。

最佳答案

这是一个有趣的观察,如果您将 onlyforgroups 更改为 groups,那么一切都会正常:

但是当你的 testng.xml 中包含多个组时,那么提到的组中的所有之前和之后的方法都会在每个测试方法之前执行。因此,为了避免这种情况,您必须将组和仅组混合在一起

说明:

如果您未在 testng xml 中指定组,则将调用所有方法。但如果您提到组,则仅执行该特定组中的方法。

这是因为如果您阅读组的定义:

https://testng.org/doc/documentation-main.html

groups The list of groups this class/method belongs to.

因此,如果您不提及组或始终运行 true,则不会调用该方法,因此您不会调用 before 和 after 方法,因为它们不在任何组中

解决方法:

您可以将两者混合为:

package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass  {
    

    @BeforeMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},groups = {"P1"})
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},groups = {"P2"})
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},groups = {"P3"})
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}

或启用始终运行 true

package driversetup;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;



public class TestBaseClass {
    

    @BeforeMethod(onlyForGroups = {"P1"},alwaysRun = true)
    public void bmeth1()
    {
        System.out.println("Before Method1 called");
    }

    @BeforeMethod(onlyForGroups = {"P2"},alwaysRun = true)
    public void bmeth2()
    {
        System.out.println("Before Method2 called");
    }

    @BeforeMethod(onlyForGroups = {"P3"},alwaysRun = true)
    public void bmeth3()
    {
        System.out.println("Before Method3 called");
    }

    @AfterMethod(onlyForGroups = {"P1"},alwaysRun = true)
    public void ameth1()
    {
        System.out.println("After Method1 called");
    }

    @AfterMethod(onlyForGroups = {"P2"},alwaysRun = true)
    public void ameth2()
    {
        System.out.println("After Method2 called");
    }

    @AfterMethod(onlyForGroups = {"P3"},alwaysRun = true)
    public void ameth3()
    {
        System.out.println("After Method3 called");
    }
    

}

这确保调用 before 和 after 方法,但仅针对正确的 @test 方法执行

关于java - @BeforeMethod/@AfterMethod(onlyForGroups)方法没有执行,如果执行属于该组的测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66263210/

相关文章:

java - 如何检查元素是否可见

java - 服务层设计,在哪里检查权限,如何处理UI层

java - 我的程序出现运行时错误 : exception in thread "main" java. lang.StringIndexOutOfBoundsException:字符串索引超出范围:27

java - selenium 驱动程序正在启动新选项卡而不是新窗口

python - 根据函数运行是否没有错误为函数分配 bool 值

java - 远程网络驱动程序无法启动 - Selenium 网格

java - 如何使用 Selenium webdriver 和 Java 读取 PDF

Java 8 32 位 - 无法初始化 JCE

java - 带有数据行的 MVC?

ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) -CentOS