TestNG 和 Selenium - 并行执行问题



我在使用 TestNG + Selenium(Chrome WebDriver)并行执行测试时遇到了一些问题。基本上,我的测试实际上似乎并没有真正并行执行。

努力实现:从 Excel 电子表格读取的数据。TestSuite.java中的所有测试用例都应在单独的WebDriver实例中为每一行数据执行 - 并行运行。例如,在单独的浏览器窗口中同时测试5个单独的登录名/密码组合。

简化图解:

public class TestSuite {
    private String param;
    @DataProvider
    public static Object[][] provider() {
        return new Object[][] {{"1"},{"2"}};
    }
    @Factory(dataProvider = "provider")
    public TestSuite(String data) {
        this.param = data;
        System.out.println(String.format("[Thread: %d] TestSuite (%s)", Thread.currentThread().getId(), param));
        // Can't initialise webdriver instances here, different thread
    }
    @BeforeClass
    public void init() {
        System.out.println(String.format("[Thread: %d] BeforeClass (%s)", Thread.currentThread().getId(), param));
        // This is where I'm retrieving webdriver instances
    }
    @Test
    public void TC_001() {
        System.out.println(String.format("[Thread: %d] TC_001 (%s)", Thread.currentThread().getId(), param));
    }
    @Test
    public void TC_002() {
        System.out.println(String.format("[Thread: %d] TC_002 (%s)", Thread.currentThread().getId(), param));
    }
}

测试NG.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="instances" group-by-instances="true">
  <test name="Test">
    <classes>
      <class name="TestSuite" />
    </classes>
  </test>
</suite>

输出:

[Thread: 1] TestSuite (1)
[Thread: 1] TestSuite (2)
[Thread: 11] BeforeClass (1)
[Thread: 11] TC_001 (1)
[Thread: 11] TC_002 (1)
[Thread: 12] BeforeClass (2)
[Thread: 12] TC_001 (2)
[Thread: 12] TC_002 (2)

这里的主要问题是我的测试显然是按顺序执行的。谁能解释我做错了什么 - 实现我上面解释的预期结果的正确方法是什么?

在TestNG中并行作为方法.xml如下所示

套件

名称="套件" 并行="方法" 按实例分组="true"

最新更新