如何并行运行测试用例?

时间:2019-03-16 20:59:54

标签: selenium selenium-webdriver testng testng-dataprovider testng.xml

我有一个public Dictionary<string,string> ListOfSections() { Dictionary<string,string> newDictionary = new Dictionary<string,string>(); IList<IWebElement> tdSectionName = sectionTable.FindElements(By.CssSelector("td.name")); foreach (IWebElement element in tdName) { IWebElement hierarchy = element.FindElement(By.CssSelector("span[data-bind='text: hierarchyId']")); IWebElement name = element.FindElement(By.CssSelector("span[data-bind='text: Name']")); newDictionary.Add(hierarchy.Text, name.Text); } return newDictionary; } 方法,并且我从@Test获取测试用例名称。我需要并行运行测试用例:

@Dataprovider

如果我想并行运行测试用例 即如果我要并行执行“开发人员团队负责人”,“质量检查”,“业务分析师”,“ DevOps Eng”,“ PMO”,我该怎么办?

5个浏览器-每个浏览器运行不同的测试用例。

TestNG XML:

@Test(dataprovider="testdataprodivder")
public void TestExecution(String arg 1)
{
/* Read the testcases from dataprovider and execute it*/
}
@Dataprovider(name="testdataprodivder")
public Object [][]Execution() throws IOException
{
return new Object[][] {{"Developer"},{"Team Lead"},{"QA"},{"Business Analyst"},{"DevOps Eng"},{"PMO"} };
}

2 个答案:

答案 0 :(得分:0)

一件事pubic并不是作用域:) -您那里还有更多不正确的语法。数据提供程序中Object后的空格不应存在,函数签名应为

public Object[][] Execution() throws IOException {
     return new Object[][] {{"Developer"},{"Team Lead"},{"QA"},{"Business Analyst"},{"DevOps Eng"},{"PMO"} };
}

接下来,您的TestExecution方法中的参数定义不正确。

public void TestExecution(String arg) {
    // Execute your tests
}

最后,只要使用DataProvider中的'p',就必须大写。这样就给我们留下了

@Test(dataProvider="testdataprovider")
public void TestExecution(String arg)
{
/* Read the testcases from dataprovider and execute it*/
}
@DataProvider(name="testdataprovider")
public Object[][] Execution() throws IOException
{
return new Object[][] {{"Developer"},{"Team Lead"},{"QA"},{"Business Analyst"},{"DevOps Eng"},{"PMO"} };
}

在这一点上,我不确定仍然存在哪些问题。这是您想要的东西吗?让我知道这是否有帮助。

答案 1 :(得分:0)

为了并行运行数据驱动的测试,您需要在parallel=true中指定@DataProvider。例如:

@Dataprovider(name="testdataprodivder", parallel=true)
public Object [][]Execution() throws IOException
{
return new Object[][] {{"Developer"},{"Team Lead"},{"QA"},{"Business Analyst"},{"DevOps Eng"},{"PMO"} };
}

要指定数据驱动测试使用的线程数,可以指定data-provider-thread-count(默认为10)。例如:

<suite name="Smoke_Test" parallel="methods" thread-count="5" data-provider-thread-count="5"> 

注意:要为数据驱动的测试外部代码动态设置并行行为,可以使用QAF-TestNG extension,在其中可以使用global.datadriven.parallel<test-case>.parallel { {3}}。