gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:23:02 27 4
gpt4 key购买 nike

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

一类

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

}

二等舱

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>

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

最佳答案

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

但实际上没有

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

testng_order_of_tests_execution

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

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。由于我们的测试单线程测试,因此我们可以得出结论,执行顺序是正确的并且符合预期。但是控制台输出混淆了。

关于java - TestNG - selenium 脚本中的测试执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47922782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com