Running test methods one after another in TestNG

前端 未结 3 1340
有刺的猬
有刺的猬 2021-01-23 05:44

I m using Eclipse + Selenium WebDriver + TestNG

This is my class structure :

class1
{
@test (invocation count =4)
method1()

@test (invocation count =4)
         


        
相关标签:
3条回答
  • 2021-01-23 06:08

    Look up "dependsOnGroups" in the documentation.

    0 讨论(0)
  • 2021-01-23 06:17

    You can also use "priority" of TestNG as:

    @Test(priority = -19)
    public void testMethod1(){
    //some code
    }
    @Test(priority = -20)
    public void testMethod2(){
    //some code
    }
    

    [Note: The priority for this test method. Lower priorities will be scheduled first]

    So, in the above example testMethod2 will be executed first as -20 less than -19

    You can visit for more details: http://testng.org/doc/documentation-main.html#annotations

    0 讨论(0)
  • 2021-01-23 06:23

    Of course there is a lot of methods to make it. An Example:

    import java.lang.reflect.Method;
    
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.Test;
    
    public class ABC {
    
        @Test(invocationCount=12)
        public void uno(){
            System.out.println("UNO");
        }
        @AfterMethod()
        public void sec(Method m){
            if(m.getName().equals("uno"))
            System.out.println("SEC");
        }
    }
    

    And suite:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="none">
      <test name="Test" parallel="none" >
        <classes>
          <class name="aaa.ABC">
              <methods>
                  <include name="uno">
              </methods>
          </class>
        </classes>
      </test> <!-- Test -->
    </suite>
    

    Remeber, if you use dependsOnMethod then thoes method will be executed after all invocation. For example:

    @Test(invocationCount=3)
    public void uno(){
        System.out.println("UNO");
    }
    @Test(dependsOnMethods={"uno"})
    public void sec(){
    
        System.out.println("SEC");
    }
    

    with:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="none">
      <test name="Test" parallel="none" >
        <classes>
          <class name="aaa.ABC">
              <methods>
                  <include name="uno"/>
                     <include name="sec"/>
              </methods>
          </class>
        </classes>
      </test> <!-- Test -->
    </suite>
    

    will give:

    UNO
    UNO
    UNO
    SEC
    
    ===============================================
    Suite
    Total tests run: 4, Failures: 0, Skips: 0
    ===============================================
    

    And if you test your tests please use verbose ="3" in suite conf. example:

    <suite name="Suite" parallel="none" verbose="3">
    

    Cause this is turning on full logs.

    0 讨论(0)
提交回复
热议问题