79

How to customize the order of execution of tests in TestNG?

For example:

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");
  }
}

In the above suite, the order of execution of tests is arbitrary. For one execution the output may be:

test1
test3
test2

How do I execute the tests in the order in which they've been written?

4

17 回答 17

88

这将起作用。

@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...)根据文档解决。

于 2013-09-10T21:38:29.673 回答
65

在 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

于 2010-04-19T18:37:51.920 回答
30

为了解决有问题的特定场景:

@Test
public void Test1() {

}

@Test (dependsOnMethods={"Test1"})
public void Test2() {

}

@Test (dependsOnMethods={"Test2"})
public void Test3() {

}
于 2013-09-08T23:33:55.980 回答
9

如果您不想@Test(priority = )在 TestNG 中使用该选项,您可以使用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);
    }
}

这基本上找出方法的行号,并按行号的升序对它们进行排序,即它们在类中定义的顺序。

于 2015-01-24T09:36:54.150 回答
8

用这个:

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注释。

于 2016-03-26T10:05:23.433 回答
6
@Test(dependsOnMethods="someBloodyMethod")
于 2013-04-22T21:01:22.983 回答
4

如果我正确理解您的问题,即您希望按指定顺序运行测试,则可以使用 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

于 2010-04-28T17:38:50.720 回答
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>

于 2015-03-06T00:51:06.363 回答
2

小猪支持 user1927494 的回答,如果你想在所有其他人之前运行一个测试,你可以这样做:

@Test()
public void testOrderDoesntMatter_1() {
}

@Test(priority=-1)
public void testToRunFirst() {
}

@Test()
public void testOrderDoesntMatter_2() {
}
于 2014-05-15T21:24:56.807 回答
1

通过使用@Test 的优先级参数,我们可以控制测试执行的顺序。

于 2012-08-21T04:23:43.177 回答
1

一个带有重要解释的答案:

“ TestNG ”有两个参数应该决定测试的执行顺序:

@Test(dependsOnGroups= "someGroup")

和:

@Test(dependsOnMethods= "someMethod")

在这两种情况下,这些功能将取决于方法或组,

但不同之处:

在这种情况下:

@Test(dependsOnGroups= "someGroup")

该方法将依赖于整个组,因此不一定会在依赖函数执行后立即执行该方法,但它可能会在运行后期甚至在其他测试运行之后发生。

重要的是要注意,如果在同一组测试中有多个使用此参数的情况下,这是解决问题的安全方法,因为整个测试集的依赖方法将首先运行,然后才运行依赖于它们的方法。

但是,在这种情况下:

@Test(dependsOnMethods= "someMethod")

即使在同一组测试中多次使用该参数,在依赖方法立即执行后,依赖方法仍然会被执行。

希望它很清楚并有所帮助。

于 2018-09-02T15:27:06.670 回答
1

类文件中方法的顺序是不可预测的,因此您需要使用依赖项或在 XML 中显式包含您的方法。

默认情况下,TestNG 将按照它们在 XML 文件中的顺序运行您的测试。如果您希望此文件中列出的类和方法以不可预测的顺序运行,请将 preserve-order 属性设置为 false

于 2015-10-08T08:32:08.617 回答
1

我遇到了同样的问题,可能的原因是由于 testng 的并行执行,解决方案是在 testng.xml 中添加 Priority 选项或简单地更新 preserve-order="true"。

<test name="Firefox Test" preserve-order="true">
于 2016-05-20T09:40:14.547 回答
0

有多种按给定顺序执行测试的方法。但是,通常情况下,测试必须是可重复的和独立的,以保证它只测试所需的功能,并且不依赖于被测试代码之外的代码的副作用。

因此,要回答您的问题,您需要提供更多信息,例如为什么按特定顺序运行测试很重要。

于 2010-04-19T17:58:52.393 回答
0

使用: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>
于 2018-03-14T12:29:13.470 回答
0

如果您碰巧使用了类似的其他内容dependsOnMethods,您可能希望在 testng.xml 文件中定义整个 @Test 流程。AFAIK,您的套件 XML 文件 (testng.xml) 中定义的顺序将覆盖所有其他排序策略。

于 2017-09-04T10:56:07.270 回答
-5

像单元测试一样的测试?做什么的?测试必须是独立的,否则......你不能单独运行测试。如果他们是独立的,为什么还要干涉?另外-如果您在多个内核上的多个线程中运行它们,那么“顺序”是什么?

于 2010-04-19T17:48:52.320 回答