TestNG - selenium 脚本中的测试执行顺序

TestNG - Order of Tests execution in selenium script

我正在使用 selenium 3.8.1 和 TestNG 6.9.2 版本,在完成 @Test 方法之前执行测试时另一个 @Test 方法开始,因此我在 selenium 脚本中遇到错误完成后测试用例执行。

一个Class

public class LoginPage{


@Test(priority=0)
public void test1(){

System.out.println(first test);
}


@Test(priority=1)
public void test2(){

System.out.println(Second test);
}

}

第二个Class

public class HomePage{


@Test(priority=0)
public void test3(){

System.out.println(first test);
}

@Test(priority=1)
public void test4(){

System.out.println(Second test);
}

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" preserve-order="true">
    <classes>
      <class name="com.tests.day.modules.LoginPage"/>
      <class name="com.tests.day.modules.HomePage"/>    
    </classes>
  </test>
</suite>

在完成登录页面 class 的测试 2 之前使用 testng.xml 文件执行上述操作后,测试 3 开始主页,因此出现异常,无法找到元素。

test 标签内使用 group-by-instances="true" testng.xml

定义您的 xml test 标签,如下所示:

<test name="Test" group-by-instances="true">

或者,您也可以查看以下代码行:

<test name="Test" preserve-order="true" group-by-instances="true">

Annotations提到TestNGpreserve-order属性如下:

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 unpredictable order, set the preserve-order attribute to false

我执行了与您的代码块相似的测试,testng.xml 如下:

  • 登录页面

    package testng_order_of_tests_execution;
    
    import org.testng.annotations.Test;
    
    public class LoginPage 
    {
    
        @Test(priority=0)
        public void test1(){
    
        System.out.println("First Test");
        }
    
    
        @Test(priority=1)
        public void test2(){
    
        System.out.println("Second Test");
        }
    }
    
  • 首页

    package testng_order_of_tests_execution;
    
    import org.testng.annotations.Test;
    
    public class HomePage 
    {
    
        @Test(priority=0)
        public void test3(){
    
        System.out.println("first test");
        }
    
        @Test(priority=1)
        public void test4(){
    
        System.out.println("second test");
        }
    }
    
  • testng.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test name="Test" preserve-order="true">
        <classes>
          <class name="testng_order_of_tests_execution.LoginPage"/>
          <class name="testng_order_of_tests_execution.HomePage"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    

我在控制台上发现的输出与您的类似,如下所示:

First Test
first test
Second Test
second test

这个Console Output显然给我们的印象是执行顺序是:

test1() -> test3() -> test2() -> test4()

但实际上没有

查看 运行 套件的 结果 您将获得如下图所示的实际执行顺序:

所以很明显实际的顺序是:

test1() -> test2() -> test3() -> test4()

琐事

您可以使用 testng-results.xml 进行更细致的观察,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="4" passed="4">
  <reporter-output>
  </reporter-output>
  <suite name="Suite" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
    <groups>
    </groups>
    <test name="Test" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
      <class name="testng_order_of_tests_execution.HomePage">
        <test-method status="PASS" signature="test3()[pri:0, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test3" duration-ms="4" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test3 -->
        <test-method status="PASS" signature="test4()[pri:1, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test4" duration-ms="1" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test4 -->
      </class> <!-- testng_order_of_tests_execution.HomePage -->
      <class name="testng_order_of_tests_execution.LoginPage">
        <test-method status="PASS" signature="test1()[pri:0, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test1" duration-ms="14" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test1 -->
        <test-method status="PASS" signature="test2()[pri:1, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test2" duration-ms="2" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test2 -->
      </class> <!-- testng_order_of_tests_execution.LoginPage -->
    </test> <!-- Test -->
  </suite> <!-- Suite -->
</testng-results>

testng-results.xml 你会发现所有的测试都是从 2017-12-25T12:57:12Z 并在 2017-12-25T12:57:12Z 结束。尽管 测试执行 花费的时间甚至少于 1 秒,但您仍然可以观察到实例名称中的差异,如 instance:testng_order_of_tests_execution.HomePage@5419f379instance:testng_order_of_tests_execution.LoginPage@735b5592。由于我们的测试是一个单线程测试,因此我们可以得出结论,执行顺序是正确的并且符合预期。但是 控制台输出 混淆了。