如何使用 Java 通过 Selenium RC 和 TestNG 顺序运行/执行方法
我有一个包含 3 个方法的 java 类:
public class Test{
public void orange(){
}
public void apple(){
}
public void mango(){
}
}
我想按顺序/顺序执行上面提到的 3 个方法,就像我用 Selenium RC 和 TestNG 编写的那样。我该怎么做?
I have a java class containing 3 methods:
public class Test{
public void orange(){
}
public void apple(){
}
public void mango(){
}
}
I want to execute 3 methods mentioned above sequentially/orderly as i have written by Selenium RC and TestNG. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是将
@Test
更改为@Test(singleThreaded=true)
。如果这样做,类中的所有测试都将在单个线程中按顺序运行。或者,
如果您想明确测试运行的顺序,则可以使用注释 @dependsOnMethods
如果您希望类中的某些(但不是全部)方法按顺序运行,这也很好。
http://testng.org/doc/documentation-main.html#dependent-methods
The easy way is to just change
@Test
to@Test(singleThreaded=true)
. If you do, all of the tests in your class will run sequentially in a single thread.Or
If you want to be explicit about the order that the tests should run in, you can use the annotation @dependsOnMethods
This is also nice if you want some, but not all, of the methods in a class to run sequentially.
http://testng.org/doc/documentation-main.html#dependent-methods
只需将
@Test
更改为@Test(singleThreaded=true)
即可。http://testng.org/javadoc/org/testng /annotations/Test.html#singleThreaded%28%29
Just change the
@Test
to@Test(singleThreaded=true)
and you're good to go.http://testng.org/javadoc/org/testng/annotations/Test.html#singleThreaded%28%29
在您的测试类中,您可以在类级别本身尝试此注释。
In your test class you try this annotation at class level itself.
我建议使用dependsOnGroups。因此,您将测试方法归为一组,并提供对该组的依赖性。所以明天如果你重构你的方法名称,你的依赖结构就不会被破坏。有关 dependentOnGroups 的更多信息,请查看此处
I suggest using dependsOnGroups. Hence you club your test method as one group and provide dependency over this group. So tomorrow if you refactor your method name, your dependency structure would not be broken. For more on dependsOnGroups look here
除了在类上使用
sequential=true
之外,您还可以为方法本身设置优先级。In addition to using
sequential=true
on the class, you can also set a priority on the methods themselves.