【问题标题】:Running test methods one after another in TestNG在TestNG中一个接一个地运行测试方法
【发布时间】:2015-02-20 21:41:56
【问题描述】:

我正在使用 Eclipse + Selenium WebDriver + TestNG

这是我的班级结构:

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

@test (invocation count =4)
method2()

}

我的 testing.xml 文件:

<classes>
<class name="tests.class1">
<methods>
<include name="method1" />
<include name="method2" />
</methods>
</class>
</classes>

运行我当前的 testing.xml 时,测试的顺序是: 方法1 方法1 方法1 方法1 方法2 方法2 方法2 方法2

但我希望按以下顺序运行测试: 方法1 方法2 方法1 方法2 方法1 方法2 方法1 方法2

请指导我达到预期的结果。 非常感谢。

【问题讨论】:

  • 你用的是Java,对吧?

标签: methods selenium-webdriver parallel-processing testng


【解决方案1】:

documentation 中查找“dependsOnGroups”。

【讨论】:

    【解决方案2】:

    您也可以使用 TestNG 的“优先级”作为:

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

    [注意:此测试方法的优先级。较低的优先级将首先安排]

    因此,在上面的示例中,testMethod2 将首先执行为 -20 小于 -19

    您可以访问以了解更多详情: http://testng.org/doc/documentation-main.html#annotations

    【讨论】:

      【解决方案3】:

      当然有很多方法可以做到。一个例子:

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

      和套件:

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

      请记住,如果您使用dependsOnMethod,那么 thoes 方法将在所有调用后执行。 例如:

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

      与:

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

      将给予:

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

      如果您测试您的测试,请在套件 conf 中使用 verbose ="3"。示例:

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

      因为这会打开完整的日志。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-20
        相关资源
        最近更新 更多