@BeforeGroups方法忽略dependsOnGroups

时间:2015-08-10 21:43:02

标签: java testng

我有一个班,有几个测试分为两组。我想要在运行分组测试时进行严格排序,以便A组中的测试先运行,然后执行B组的设置方法,然后运行B组。例如:

@Test(groups="A")
public void a1() {
    // ...
}

@Test(groups="A")
public void a2() {
    // ...
}

@BeforeGroups(value="B", dependsOnGroups="A")
public void setupB() {
    // ...
}

@Test(groups="B")
public void b1() {
    // ...
}

@Test(groups="B")
public void b2() {
    // ...
}

我遇到的问题是,TestNG似乎并不尊重setupB方法。而不是预期的执行顺序:
A1 / A2
A2 / A1
setupB
B1 / B2
b2 / b1

执行类似这样的事情:
A1
setupB
B1
A2
B2

知道我在这个设置上做错了什么吗?我是否在概念上遗漏了有关TestNG小组如何运作的内容?

3 个答案:

答案 0 :(得分:1)

尝试为测试方法指定dependsOnGroups

public class TestClass {

    @Test(groups="B")
    public void b1() {
        System.out.println("b1");
    }

    @Test(groups="B")
    public void b2() {
        System.out.println("b2");
    }

    @Test(groups="A", dependsOnGroups="B")
    public void a1() {
        System.out.println("a1");
    }

    @Test(groups="A", dependsOnGroups="B")
    public void a2() {
        System.out.println("a2");
    }

    @BeforeGroups(value="A", dependsOnGroups="B")
    public void setupA() {
        System.out.println("before");
    }
}

我可能错了,但似乎如果某个属于某个组的测试方法已被选中执行并且它不依赖于任何组或方法,那么它只会导致@BeforeGroups - 带注释的方法运行(忽略那里指定的dependsOnGroups)。请注意,如果没有一些明确的声明,TestNG不保证执行顺序,例如使用“依赖”或“优先”机制。

希望Cedric Beust能够访问这个问题。

答案 1 :(得分:0)

如果可行,这可能是现在的解决方法。不确定是否可以同时使用两个注释。

@Test(groups="A")

public void a1() {
    // ...
}

@Test(groups="A")
public void a2() {
    // ...
}

@BeforeGroups(value="B")
@AfterGroups(value="A")
public void setupB() {
    // ...
}

@Test(groups="B")
public void b1() {
    // ...
}

@Test(groups="B")
public void b2() {
    // ..

。 }

答案 2 :(得分:0)

如前所述,当调用测试方法时,它仅检查该方法所属的测试组是否已执行相应的@BeforeGroups批注方法,如果尚未执行,则TestNG仅调用该方法。 @BeforeGroups方法。

解决方法是添加自定义IMethodInterceptor,这将添加对@BeforeGroups#dependsOnGroups中与同一组相关的测试方法的依赖关系:

public class TestMethodInterceptor implements IMethodInterceptor {

    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        if (!(context instanceof TestRunner)) {
            return methods;
        }

        TestRunner testRunner = (TestRunner) context;
        Collection<ITestClass> testClasses = testRunner.getTestClasses();
        Map<String, List<String>> groupDependencies = MethodGroupsHelper.findGroupsMethods(testClasses, true).entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        groupBeforeMethods -> groupBeforeMethods.getValue().stream()
                                .flatMap(m -> Arrays.stream(m.getGroupsDependedUpon()))
                                .collect(Collectors.toList())
                ));

        return methods.stream()
                .map(IMethodInstance::getMethod)
                .map(method -> {
                    Set<String> methodGroupDependencies = Stream.ofNullable(method.getGroups())
                            .flatMap(Arrays::stream)
                            .flatMap(group -> groupDependencies.getOrDefault(group, List.of()).stream())
                            .collect(Collectors.toSet());
                    return methodGroupDependencies.isEmpty() ? method : addGroupDependencies(method, methodGroupDependencies);
                })
                .map(MethodInstance::new)
                .collect(Collectors.toList());

    }

    private ITestNGMethod addGroupDependencies(ITestNGMethod method, Collection<String> groups) {
        String[] methodGroupDependencies;
        if (method.getGroupsDependedUpon() == null || method.getGroupsDependedUpon().length == 0) {
            methodGroupDependencies = groups.toArray(new String[0]);
        } else {
            methodGroupDependencies = Stream.concat(Arrays.stream(method.getGroupsDependedUpon()), groups.stream())
                    .distinct()
                    .toArray(String[]::new);
        }

        return new WrappedTestNGMethod(method) {
            @Override
            public String[] getGroupsDependedUpon() {
                return methodGroupDependencies;
            }
        };
    }

}

TestMethodInterceptor是一个侦听器,可以通过@Listeners批注添加到执行中。

相关问题