特定组的 TestNG @AfterMethod 在 Eclipse 中的每个方法之后运行

发布于 2024-11-14 07:48:21 字数 1086 浏览 0 评论 0原文

我不明白 TestNG 中的分组是如何工作的,我有这样的代码:

    @AfterMethod(groups = { "refreshPage" })
    public void refresh() throws IOException {
        driver.navigate().refresh();
    }

    @Test
    public void test1() {

    }

    @Test(groups = { "refreshPage" })
    public void test2() {

    }

并且 @AfterMethod 在它们两个之后运行,即使只有第二个是该组的一部分。我猜它与 eclipse 执行时生成的 Template testng.xml 配置文件无关。它只是整个测试类或单个测试方法的准系统设置。所以它不应该影响分组

编辑:

所以为了让 @AfterMethod 仅在某些方法之后运行并运行整个测试类,我必须运行它两次,有两组方法(有/没有刷新)。

<test name="Test1">
  <groups>
    <run>
      <include name="refreshPage*"/>
    </run>
  </groups>

  <classes>
    <class name="example.Test"/>
  </classes>
</test>

<test name="Test2">
  <groups>
    <run>
      <include name="dontRefreshPage*"/>
    </run>
  </groups>

  <classes>
    <class name="example.Test"/>
  </classes>
</test>

我不知道如何使其在通过 Eclipse 启动时工作,因为 在模板中被替换。

I don't get how the grouping works in TestNG, I have this code :

    @AfterMethod(groups = { "refreshPage" })
    public void refresh() throws IOException {
        driver.navigate().refresh();
    }

    @Test
    public void test1() {

    }

    @Test(groups = { "refreshPage" })
    public void test2() {

    }

And the @AfterMethod is run after both of them, even if only the second one is part of that group. I guess it doesn't relate to the Template testng.xml configuration file that is generated by eclipse for the execution. It's just a barebone setting for either the entire test class or individual test method. So it shouldn't influence groupings

EDITED:

So that in order to have @AfterMethod run after some methods only and run the entire test class, I have to run it twice, having two groups of methods (with/without refreshing).

<test name="Test1">
  <groups>
    <run>
      <include name="refreshPage*"/>
    </run>
  </groups>

  <classes>
    <class name="example.Test"/>
  </classes>
</test>

<test name="Test2">
  <groups>
    <run>
      <include name="dontRefreshPage*"/>
    </run>
  </groups>

  <classes>
    <class name="example.Test"/>
  </classes>
</test>

I have no idea how to make it work when started via Eclipse, cause the <test> are replaced in the Template.

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

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

发布评论

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

评论(1

姐不稀罕 2024-11-21 07:48:21

抱歉,我不明白你对问题的描述。您的两个测试方法属于“刷新”组,因此如果您运行该组,您将看到它们都运行,并且每个方法后面都会调用prepareTest()(顺便说一句,这似乎很奇怪after 方法将被称为“prepareTest()”,您确定这不是一个@BeforeMethod 吗?)。

不过,您的代码有一些奇怪的地方:您的两个测试方法声明了一个数据提供程序,但它们不接受任何参数。它们要么需要参数,所以您应该更新它们的签名以匹配数据提供者返回的内容,要么它们不需要参数,并且您应该删除 dataProvider 属性。

I'm sorry but I don't understand your description of the problem. Your two test methods belong to the group "refresh", so if you run that group, you will see both of them run, and each of them will be followed by a call to prepareTest() (by the way, it seems odd that an after method would be called "prepareTest()", are you sure you don't mean for this to be a @BeforeMethod?).

There is something odd about your code, though: your two test methods declare a data provider but they don't take any argument. Either they need arguments, so you should update their signature to match what the data providers return, or they don't, and you should remove the dataProvider attribute.

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