gpt4 book ai didi

java - 自定义使用 JUnit @Parameters 运行哪些测试?

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:20 25 4
gpt4 key购买 nike

我有以下代码,其中 listOne 中的每个 url 均使用方法 testItem 进行测试:

@Parameters(name="{0}")
public static Collection<Object[]> data() throws Exception {
final Set<String> listOne = getListOne();
final Collection<Object[]> data = new ArrayList<>();
for (final String url : listOne) {
data.add(new Object[] { url });
}
return data;
}

@Test
public void testItem() {
driverOne.makeDecision(urlToTest);
assertTrue(driverOne.success(urlToTest);
}

如果我现在想要添加第二个列表 listTwo,并仅对 listTwo 的项目(而不是 listOne)运行如下定义的测试方法,该怎么办?

@Test
public void testItemAlternate() {
driverTwo.makeDecision(urlToTest);
assertTrue(driverTwo.success(urlToTest));
}

也就是说,我希望 driverOnelistOne 中的所有 URL 做出决定,并希望 driverTwolistTwo 中的所有 URL 做出决定。将其转换为代码的最佳方法是什么?谢谢。

最佳答案

引用自:https://github.com/junit-team/junit/wiki/Parameterized-tests

The custom runner Parameterized implements parameterized tests. When running a parameterized test class, instances are created for the cross-product of the test methods and the test data elements.

因此,我认为不,这是不可能的。

如果你想做这样的事情,我猜你要么

(1) 需要构造两个测试类,一个用于要使用第一个集合执行的每个测试,一个用于要使用第二个集合执行的每个测试,或者

(2) 将需要使用除 @Parameters 注释之外的另一种机制,可能是手工制作的。

关于java - 自定义使用 JUnit @Parameters 运行哪些测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25001020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com