在同一组测试用例上运行多个测试数据

发布于 2024-07-28 23:31:08 字数 292 浏览 4 评论 0原文

我是日食新手。 我正在使用 JUnit 4,并且我在我的类中编写了一个设置方法,该方法扩展了发生一些初始化的 Testcase。 我在同一个班级中有一些测试用例。 我有压缩形式的测试数据并附加到工作空间。 目前我能够针对单个测试数据运行所有测试用例。 不知何故,我希望控件返回到 setup() 以获取第二个测试数据并运行所有测试用例。 是否可以? 如果是的话,有人可以发送一些代码片段吗?

提前感谢

您的回复,但是我应该在哪里保存这样的代码,是否应该将其保存在设置方法中以及如何从设置中获取测试数据?

I am new to eclipse. I am using JUnit 4. and i have written a set up method in my class which extends Testcase where some initialization happens. I have some set of testcases in the same class. I have test data in zipped form and attached to work space.
Currently i am able to run all test cases for a single test data. Somehow i want the control to go back to set up() to take second test data and run all the test cases.
Is it possible? ans if yes can anyone please send some code snippet?

Thanks in advance

Thanks for the reply but where should i keep such code whether it should be kept in set up method and how test data will be taken up from set up?

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

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

发布评论

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

评论(1

毁虫ゝ 2024-08-04 23:31:08

您需要使用参数化运行器。 它允许您使用多个测试数据运行相同的测试。 例如,以下内容意味着测试将运行四次,参数“number”每次更改为数组中的值。

@RunWith(value = Parameterized.class)
public class StackTest {
 Stack<Integer> stack;
 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }
 ...
}

编辑

不确定有什么不清楚,但我会尝试澄清。

需要 @RunWith(value = Parameterized.class) 注释。 您必须有一个用 @Parameters 注释的方法,该方法返回一个 Collection 对象,该对象的每个元素必须是用于测试的各种参数的数组。 您必须有一个接受这些参数的公共构造函数。

其他信息和另一个示例可以在文档中找到。

更多示例

You need to use the Parameterized runner. It allows you to run the same test with multiple test data. e.g. The following will imply that the tests will run four times, with the parameter "number" changed each time to the value in the array.

@RunWith(value = Parameterized.class)
public class StackTest {
 Stack<Integer> stack;
 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }
 ...
}

Edit

Not sure what isn't clear, but I'll attempt to clarify.

The @RunWith(value = Parameterized.class) annotation is required. You must have a method annotated with @Parameters that returns a Collection object, each element of which must be an Array of the various parameters used for the test. You must have a public constructor that will accept these parameters.

Additional information, and another example can be found in the documentation.

Even more examples.

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