【问题标题】:How to add Grouping and Dependency in @BeforeMethod On TestNG如何在 TestNG 的 @BeforeMethod 中添加分组和依赖
【发布时间】:2014-03-19 05:59:28
【问题描述】:

有什么方法可以在@Test 上赋予@BeforeMethod 的依赖关系,因为在我的场景中,不同的TestMethod 有不同的设置,而我需要一个依赖于TestMethod 的设置。我在这里添加一些代码 sn-p 以便更好地理解

@BeforeMethod(groups = {"gp2"})
public void setUp1() {
    System.out.println("SetUp 1 is done");
}

@BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
public void setUp2() {
    System.out.println("SetUp 2 is done");
}

@Test(timeOut = 1, groups = {"tgp1"})
public void testMethod() throws InterruptedException {
    Thread.sleep(2000);
    System.out.println("TestMethod() From Group1");
}

@Test(dependsOnMethods = {"testMethod"}, groups = {"tgp2"})
public void anotherTestMethod() {
    System.out.println("AnotherTestMethod()From Group1 and Group2");
}

输出

设置 1 已完成 设置 2 完成

但我需要 setUp1() 而不是 setUp2() 因为它取决于 tgp1 组。

我观察到的另一件事是,如果我从

更改依赖项
@BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})

到

@BeforeMethod(groups = {"gp1"}, dependsOnMethods = {"testMethod"}) 

然后我得到一个异常

 setUp2() is depending on method public void testMethod() throws java.lang.InterruptedException, which is not annotated with @Test or not included.

我需要执行的应该是这个步骤

SetUp1---->testMethod1()-------->SetUp2--------->testMethod2()

我需要这个,因为不同的 TestMethod 有不同的工作,它有不同的 SetUp()。

【问题讨论】:

  • 你能解释一下你想用这个依赖实现什么吗?如果您可以添加测试启动方式的配置或说明,也会有所帮助。

标签: java unit-testing selenium-webdriver testng


【解决方案1】:

在您的情况下,您可以简单地从各自的测试方法调用各自的 setUp 方法,因为每个测试实际上没有共同的@BeforeMethod。

或者您可以将这两个测试分成不同的测试类,每个类都有一个@BeforeMethod。

或者您可以使用如下@BeforeMethod 方法根据测试方法名称进行条件设置调用。这里@BeforeMethod可以声明java.lang.reflect.Method类型的参数。此参数将接收此 @BeforeMethod 完成后将调用的测试方法

@BeforeMethod
public void setUp(Method method) {
    if (method.getName().equals("testMethod")) {
        setUp1();
    } else if (method.getName().equals("anotherTestMethod")) {
        setUp2();
    }
}

public void setUp2() {
    System.out.println("SetUp 2 is done");
}

public void setUp1() {
    System.out.println("SetUp 1 is done");
}    

【讨论】:

  • 感谢您的帮助我已经这样做以逃避这种情况----:) 但我的问题不是以这种方式逃避我使用@BeforeMethod 并做我想做的事情,因为按照TestNG 框架文档提供了这种方法来服务测试方法你能帮我理解这个的正确原因......再次感谢你的帮助。
  • 用 Method 参数更新了答案。希望这会有所帮助
  • 是的,我真的需要这种方式,非常感谢,非常感谢。哦,太好了,我希望你这样做。
【解决方案2】:

@BeforeMethod 的目的是在使用@Test 注释的每个测试方法之前运行并进行一些设置工作。因此,为什么要创建以另一种方式工作的依赖项有点令人困惑——在设置方法之前执行测试方法。 通常的方法如下:

@BeforeMethod
public void setUp() {
}

@Test
public void testMethod1() {
}

@Test
public void testMethod2() {
}

最有可能生成以下执行列表:

设置()
testMethod1()
设置()
测试方法2()

如果有多个@BeforeMethods,它们都将在@Test方法之前运行。

现在,如果您想运行不同的组,您可以使用不同的组注释不同的方法并指定要运行的组。但是,为此您需要提供 testng.xml 或指定要执行的组。


编辑(基于附加评论):

我会建议以下方法之一:

  1. 将测试方法分成不同的类并拥有@BeforeMethod 在每个班级执行所需的设置。这尤其有意义,如果 测试涵盖不同的领域。
  2. 定义不同的组和 将相应的测试和设置方法放在同一组中。这个 选项可让您将设置方法与测试方法混合搭配。

您的问题中有些内容不清楚,但如果相关,可以从建议的方法中受益:

  • 是否需要依赖?
  • 顺序或测试重要吗?

【讨论】:

  • 感谢您的帮助........我非常感谢您的 cmets 和建议实际上我需要这个,因为不同的 TestMethods 有不同的工作并且它有不同的 SetUp() 我需要 SetUp1()- -----testMethod()--------SetUp2()---------testMethod2()....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 2015-07-18
  • 2018-11-21
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多