运行 @BeforeMethod testNG 注释,当方法*包含*一个组时
Running a @BeforeMethod testNG annotation when the method *contains* a group
如果我有一个 beforeMethod 和一个组,我 运行 另一个组,但在那个组中存在一个测试,它同时包含我 运行ning 的组作为使用 beforeMethod 的组,我希望该测试 运行 它的 beforemethod。例如:
@BeforeMethod(groups = "a")
public void setupForGroupA() {
...
}
@Test(groups = {"supplemental", "a"})
public void test() {
...
}
当我 运行 testNG with groups=supplemental 时,我仍然希望 beforeMethod 在测试之前为 运行,但是因为组是补充而不是 'a',它不会.
这对我来说似乎是一个非常明显的特征,我觉得我一定是错误地使用了组,所以我也想解释一下我的工作流程,以防我的问题出在这里。
我正在使用组来定义不同的测试层,以及他们是否需要创建自己的帐户,或者他们是否需要使用代理来访问他们的数据等。我将有组smoke、supplemental 和 regression 以及 uniqueAccount、proxy 等组。我不需要为第一个分组进行特定设置,但这些是我在 maven 中传递给 运行 的组。我需要为后面的组进行特定设置,但我不想 运行 只是需要代理或需要唯一帐户的测试。
如果我没记错的话,你每次都想 运行 你的 before 方法。在这种情况下,您可以像这样为您的 before 方法设置 alwaysRun=true-
@BeforeMethod(alwaysRun = true, groups = "a")
public void setupForGroupA() {
...
}
这是您想要的解决方案之一。
组配置未在 运行 时评估。 test
方法不会激活 setupForGroupA
方法。
该功能用于查找您想要的方法运行。
根据下面的例子:
@BeforeMethod(groups = "a")
public void setupForGroupA() {
...
}
@Test(groups = {"supplemental", "a"})
public void test() {
...
}
@Test(groups = {"supplemental"})
public void test2() {
...
}
如果你 运行 这个 class 与组 "a" 它将 运行 setupForGroupA
和 test
方法,因为它们被标记为组 "a".
如果你 运行 这个 class 组 "supplemental" 它将 运行 test
和 test2
方法,因为它们被标记为组 "supplemental".
看起来你对某些方法有不同的行为,所以一个好的方法是通过 class 而不是 [=49= 来分离不同 classes 和 select 测试中的方法] 分组测试。
public class A {
@BeforeMethod
public void setupForGroupA() {
...
}
@Test
public void test() {
...
}
}
和
public class Supplemental {
@Test
public void test2() {
...
}
}
运行 class 只会 运行 setupForGroupA
和 test
。
运行 class 仅补充遗嘱 运行 test2
。
运行 两个 class 都会 运行 一切。
如果你想 运行 同时 classes 和过滤其他东西,你可以用 a method interceptor:
实现你自己的逻辑
@MyCustomAnnotation(tags = "a", "supplemental")
public class A {
...
}
@MyCustomAnnotation(tags = "supplemental")
public class Supplemental {
...
}
public class MyInterceptor implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
// for each method, if its class contains the expected tag, then add it to the list
// expect tag can be passed by a system property or in a parameter from the suite file (available from ITestContext)
}
}
如果我有一个 beforeMethod 和一个组,我 运行 另一个组,但在那个组中存在一个测试,它同时包含我 运行ning 的组作为使用 beforeMethod 的组,我希望该测试 运行 它的 beforemethod。例如:
@BeforeMethod(groups = "a")
public void setupForGroupA() {
...
}
@Test(groups = {"supplemental", "a"})
public void test() {
...
}
当我 运行 testNG with groups=supplemental 时,我仍然希望 beforeMethod 在测试之前为 运行,但是因为组是补充而不是 'a',它不会.
这对我来说似乎是一个非常明显的特征,我觉得我一定是错误地使用了组,所以我也想解释一下我的工作流程,以防我的问题出在这里。
我正在使用组来定义不同的测试层,以及他们是否需要创建自己的帐户,或者他们是否需要使用代理来访问他们的数据等。我将有组smoke、supplemental 和 regression 以及 uniqueAccount、proxy 等组。我不需要为第一个分组进行特定设置,但这些是我在 maven 中传递给 运行 的组。我需要为后面的组进行特定设置,但我不想 运行 只是需要代理或需要唯一帐户的测试。
如果我没记错的话,你每次都想 运行 你的 before 方法。在这种情况下,您可以像这样为您的 before 方法设置 alwaysRun=true-
@BeforeMethod(alwaysRun = true, groups = "a")
public void setupForGroupA() {
...
}
这是您想要的解决方案之一。
组配置未在 运行 时评估。 test
方法不会激活 setupForGroupA
方法。
该功能用于查找您想要的方法运行。 根据下面的例子:
@BeforeMethod(groups = "a")
public void setupForGroupA() {
...
}
@Test(groups = {"supplemental", "a"})
public void test() {
...
}
@Test(groups = {"supplemental"})
public void test2() {
...
}
如果你 运行 这个 class 与组 "a" 它将 运行 setupForGroupA
和 test
方法,因为它们被标记为组 "a".
如果你 运行 这个 class 组 "supplemental" 它将 运行 test
和 test2
方法,因为它们被标记为组 "supplemental".
看起来你对某些方法有不同的行为,所以一个好的方法是通过 class 而不是 [=49= 来分离不同 classes 和 select 测试中的方法] 分组测试。
public class A {
@BeforeMethod
public void setupForGroupA() {
...
}
@Test
public void test() {
...
}
}
和
public class Supplemental {
@Test
public void test2() {
...
}
}
运行 class 只会 运行 setupForGroupA
和 test
。
运行 class 仅补充遗嘱 运行 test2
。
运行 两个 class 都会 运行 一切。
如果你想 运行 同时 classes 和过滤其他东西,你可以用 a method interceptor:
实现你自己的逻辑@MyCustomAnnotation(tags = "a", "supplemental")
public class A {
...
}
@MyCustomAnnotation(tags = "supplemental")
public class Supplemental {
...
}
public class MyInterceptor implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
// for each method, if its class contains the expected tag, then add it to the list
// expect tag can be passed by a system property or in a parameter from the suite file (available from ITestContext)
}
}