[英]Order of execution of tests in TestNG
如何自定义TestNG中测试的执行顺序?
例如:
public class Test1 {
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
@Test
public void test3() {
System.out.println("test3");
}
}
在上面的套件中,测试的执行顺序是任意的。 对于一次执行,output 可能是:
test1
test3
test2
如何按照编写的顺序执行测试?
这将起作用。
@Test(priority=1)
public void Test1() {
}
@Test(priority=2)
public void Test2() {
}
@Test(priority=3)
public void Test3() {
}
priority
鼓励执行顺序,但不保证前一个优先级已经完成。 test3
可以在test2
完成之前启动。 如果需要保证,则声明一个依赖项。
与声明依赖项的解决方案不同,即使一个测试失败,使用priority
测试也会执行。 根据文档,可以使用@Test(...alwaysRun = true...)
解决此依赖关系问题。
在 TestNG 中,您使用dependsOnMethods 和/或dependsOnGroups:
@Test(groups = "a")
public void f1() {}
@Test(groups = "a")
public void f2() {}
@Test(dependsOnGroups = "a")
public void g() {}
在这种情况下,g() 只会在 f1() 和 f2() 完成并成功后运行。
你会在文档中找到很多例子: http : //testng.org/doc/documentation-main.html#test-groups
要解决有问题的特定场景:
@Test
public void Test1() {
}
@Test (dependsOnMethods={"Test1"})
public void Test2() {
}
@Test (dependsOnMethods={"Test2"})
public void Test3() {
}
如果您不想在 TestNG 中使用@Test(priority = )
选项,您可以利用javaassist库和 TestNG 的IMethodInterceptor
根据测试类中定义测试方法的顺序对测试进行优先级排序。 这是基于此处提供的解决方案。
将此侦听器添加到您的测试类:
package cs.jacob.listeners;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
public class PriorityInterceptor implements IMethodInterceptor {
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
Comparator<IMethodInstance> comparator = new Comparator<IMethodInstance>() {
private int getLineNo(IMethodInstance mi) {
int result = 0;
String methodName = mi.getMethod().getConstructorOrMethod().getMethod().getName();
String className = mi.getMethod().getConstructorOrMethod().getDeclaringClass().getCanonicalName();
ClassPool pool = ClassPool.getDefault();
try {
CtClass cc = pool.get(className);
CtMethod ctMethod = cc.getDeclaredMethod(methodName);
result = ctMethod.getMethodInfo().getLineNumber(0);
} catch (NotFoundException e) {
e.printStackTrace();
}
return result;
}
public int compare(IMethodInstance m1, IMethodInstance m2) {
return getLineNo(m1) - getLineNo(m2);
}
};
IMethodInstance[] array = methods.toArray(new IMethodInstance[methods.size()]);
Arrays.sort(array, comparator);
return Arrays.asList(array);
}
}
这基本上找出方法的行号,并按行号的升序对它们进行排序,即它们在类中定义的顺序。
用这个:
public class TestNG
{
@BeforeTest
public void setUp()
{
/*--Initialize broowsers--*/
}
@Test(priority=0)
public void Login()
{
}
@Test(priority=2)
public void Logout()
{
}
@AfterTest
public void tearDown()
{
//--Close driver--//
}
}
通常 TestNG 提供了许多注解,我们可以使用@BeforeSuite, @BeforeTest, @BeforeClass
来初始化浏览器/设置。
如果您在脚本中编写了大量测试用例并希望按照指定的优先级执行,我们可以分配优先级,然后使用: @Test(priority=0)
从 0,1,2,3...开始。
同时我们可以将多个测试用例分组并通过分组执行。 为此,我们将使用@Test(Groups='Regression')
最后就像关闭浏览器一样,我们可以使用@AfterTest, @AfterSuite, @AfterClass
注释。
@Test(dependsOnMethods="someBloodyMethod")
如果我正确理解您的问题,因为您想以指定的顺序运行测试,则可以使用 TestNG IMethodInterceptor。 查看http://beust.com/weblog2/archives/000479.html了解如何利用它们。
如果您想运行一些预初始化,请查看 IHookable http://testng.org/javadoc/org/testng/IHookable.html和相关线程http://groups.google.com/group/testng-users/browse_thread/线程/42596505990e8484/3923db2f127a9a9c?lnk=gst&q=IHookable#3923db2f127a9a9c
捎带用户1927494的回答,如果你想在所有其他人之前运行一个测试,你可以这样做:
@Test()
public void testOrderDoesntMatter_1() {
}
@Test(priority=-1)
public void testToRunFirst() {
}
@Test()
public void testOrderDoesntMatter_2() {
}
通过在 testNg.xml 中指定要执行的测试方法,我们可以按所需顺序执行测试用例
<suite> <test name="selenium1"> <classes> <class name="com.test.SeleniumTest" > <methods><include name="methodB"></include> <include name="methodA"></include> </methods> </class> </classes> </test> </suite>
通过对@Test 使用优先级参数,我们可以控制测试执行的顺序。
类文件中方法的顺序是不可预测的,因此您需要使用依赖项或在 XML 中显式包含您的方法。
默认情况下,TestNG 将按照在 XML 文件中找到的顺序运行您的测试。 如果您希望此文件中列出的类和方法以不可预测的顺序运行,请将 preserve-order 属性设置为 false
我遇到了同样的问题,可能的原因是 testng 的并行执行,解决方案是在 testng.xml 中添加 Priority 选项或简单地更新 preserve-order="true"。
<test name="Firefox Test" preserve-order="true">
一个带有重要解释的答案:
“ TestNG ”有两个参数应该确定测试的执行顺序:
@Test(dependsOnGroups= "someGroup")
和:
@Test(dependsOnMethods= "someMethod")
在这两种情况下,这些功能将取决于方法或组,
但区别在于:
在这种情况下:
@Test(dependsOnGroups= "someGroup")
该方法将依赖于整个组,因此不一定在依赖函数执行后立即执行该方法,但可能会在运行的后期甚至其他测试运行后发生。
重要的是要注意,在此参数的同一组测试中有多个用途的情况下,这是解决问题的安全方法,因为整个测试组的依赖方法将首先运行,然后才运行依赖于它们的方法。
但是,在这种情况下:
@Test(dependsOnMethods= "someMethod")
即使在同一组测试中多次使用此参数,在立即执行依赖方法后仍会执行依赖方法。
希望它很清楚并有所帮助。
有多种方法可以按给定的顺序执行测试。 但是,通常情况下,测试必须是可重复和独立的,以保证它只测试所需的功能,而不依赖于被测试内容之外的代码的副作用。
因此,要回答您的问题,您需要提供更多信息,例如为什么按特定顺序运行测试很重要。
如果您碰巧使用了诸如dependsOnMethods
类的其他内容,您可能需要在testng.xml 文件中定义整个@Test 流程。 AFAIK,您的套件 XML 文件 (testng.xml) 中定义的顺序将覆盖所有其他排序策略。
使用:preserve-order="true" enabled="true" 以您编写的方式运行测试用例。
<suite name="Sanity" verbose="1" parallel="" thread-count="">
<test name="Automation" preserve-order="true" enabled="true">
<listeners>
<listener class-name="com.yourtest.testNgListner.RetryListener" />
</listeners>
<parameter name="BrowserName" value="chrome" />
<classes>
<class name="com.yourtest.Suites.InitilizeClass" />
<class name="com.yourtest.Suites.SurveyTestCases" />
<methods>
<include name="valid_Login" />
<include name="verifyManageSurveyPage" />
<include name="verifySurveyDesignerPage" />
<include name="cloneAndDeleteSurvey" />
<include name="createAndDelete_Responses" />
<include name="previewSurvey" />
<include name="verifySurveyLink" />
<include name="verifySurveyResponses" />
<include name="verifySurveyReports" />
</methods>
</classes>
</test>
</suite>
testNG中的执行顺序如下。
beforeSuite,beforeTest,beforeClass,beforeMethod,测试,afterMethod,afterClass,afterTest,afterSuite
像单元测试一样的测试? 做什么的? 测试必须是独立的,否则......你不能单独运行测试。 如果他们是独立的,为什么还要干涉? 另外 - 如果您在多个内核上的多个线程中运行它们,什么是“顺序”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.