programing

TestNG에서 테스트 실행 순서

nasanasas 2020. 11. 3. 08:07
반응형

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

위의 모음에서 테스트 실행 순서는 임의적입니다. 한 번의 실행에 대한 출력은 다음과 같습니다.

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() {

}

이것을 사용하십시오 :

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

이것은 기본적으로 메서드의 줄 번호를 찾아서 해당 줄 번호의 오름차순, 즉 클래스에서 정의 된 순서로 정렬합니다.


@Test(dependsOnMethods="someBloodyMethod")

지정된 순서로 테스트를 실행하고 싶다는 질문을 올바르게 이해하면 TestNG IMethodInterceptor를 사용할 수 있습니다. 이를 활용하는 방법에 대해서는 http://beust.com/weblog2/archives/000479.html 을 참조 하십시오 .

If you want run some preinitialization, take a look at IHookable http://testng.org/javadoc/org/testng/IHookable.html and associated thread http://groups.google.com/group/testng-users/browse_thread/thread/42596505990e8484/3923db2f127a9a9c?lnk=gst&q=IHookable#3923db2f127a9a9c


By Specifying test methods to be executed in testNg.xml we can execute the test cases in desired order

<suite>
<test name="selenium1">
 		<classes>
			<class name="com.test.SeleniumTest" >
			    <methods><include name="methodB"></include>
			        <include name="methodA"></include>
			    </methods>    
			 </class>
		</classes>
 
	</test>
</suite>


By using priority paramenter for @Test we can control the order of test execution.


Piggy backing off of user1927494's answer, In case you want to run a single test before all others, you can do this:

@Test()
public void testOrderDoesntMatter_1() {
}

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

@Test()
public void testOrderDoesntMatter_2() {
}

The ordering of methods in the class file is unpredictable, so you need to either use dependencies or include your methods explicitly in XML.

By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictible order, set the preserve-order attribute to false


I've faced the same issue, the possible reason is due to parallel execution of testng and the solution is to add Priority option or simply update preserve-order="true" in your testng.xml.

<test name="Firefox Test" preserve-order="true">

There are ways of executing tests in a given order. Normally though, tests have to be repeatable and independent to guarantee it is testing only the desired functionality and is not dependent on side effects of code outside of what is being tested.

So, to answer your question, you'll need to provide more information such as WHY it is important to run tests in a specific order.


In case you happen to use additional stuff like dependsOnMethods, you may want to define the entire @Test flow in your testng.xml file. AFAIK, the order defined in your suite XML file (testng.xml) will override all other ordering strategies.


use: preserve-order="true" enabled="true" that would run test cases in the manner in which you have written.

<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>

An answer with an important explanation:

There are two parameters of "TestNG" who are supposed to determine the order of execution the tests:

@Test(dependsOnGroups= "someGroup")

And:

@Test(dependsOnMethods= "someMethod")

In both cases these functions will depend on the method or group,

But the differences:

In this case:

@Test(dependsOnGroups= "someGroup")

The method will be dependent on the whole group, so it is not necessarily that immediately after the execution of the dependent function, this method will also be executed, but it may occur later in the run and even after other tests run.

It is important to note that in case and there is more than one use within the same set of tests in this parameter, this is a safe recipe for problems, because the dependent methods of the entire set of tests will run first and only then the methods that depend on them.

However, in this case:

@Test(dependsOnMethods= "someMethod")

Even if this parameter is used more than once within the same set of tests, the dependent method will still be executed after the dependent method is executed immediately.

Hope it's clearly and help.


Tests like unit tests? What for? Tests HAVE to be independant, otherwise.... you can not run a test individually. If they are independent, why even interfere? Plus - what is an "order" if you run them in multiple threads on multiple cores?

참고URL : https://stackoverflow.com/questions/2669576/order-of-execution-of-tests-in-testng

반응형