1、使用线程池运行所有的测试用例(methods)
将parallel设置为methods,线程个数设置为5
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="methods" thread-count="5">
<test name="my">
<classes>
<class name="day03.MyTest" />
</classes>
</test>
<test name="my2">
<classes>
<class name="day03.MyTest2" />
</classes>
</test>
</suite>
2、使用线程池运行所有的测试集(tests)
将parallel设置为tests,线程个数设置为5
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests" thread-count="5">
<test name="my">
<classes>
<class name="day03.MyTest" />
</classes>
</test>
<test name="my2">
<classes>
<class name="day03.MyTest2" />
</classes>
</test>
</suite>
3、使用线程池运行测试集里面的所有的测试类(classes)
将parallel设置为classes,线程个数设置为5
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="classes" thread-count="5">
<test name="my">
<classes>
<class name="day03.MyTest" />
<class name="day03.MyTest2" />
</classes>
</test>
</suite>
4、使用线程池(默认10个线程)运行参数化测试
将@DataProvider注解里面的parallel设置为true
package day03;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MyTest3 {
@Test(dataProvider="dp")
public void f7(String username,String password) throws Exception {
System.out.println(username);
Thread.sleep(1000);
System.out.println(password);
}
@DataProvider(parallel=true)
public Object[][] dp(){
Object[][] arr = {
{"admin","123456"},
{"test","123"},
{"","123456"},
{"test",""}
};
return arr;
}
}
将线程池的线程数量设为3
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" data-provider-thread-count="3">
<test name="my">
<classes>
<class name="day03.MyTest3" />
</classes>
</test>
</suite>
5、使用线程池模拟并发操作
使用5个线程同时执行该测试,累计执行次数达到5次结束
package day03;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MyTest3 {
@Test(dataProvider="dp", threadPoolSize=5, invocationCount=5)
public void f7(String keyword) throws Exception {
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet("https://search.jd.com/Search?keyword="+keyword);
HttpResponse response = client.execute(request);
System.out.println(response);
}
@DataProvider(parallel=false)
public Object[][] dp(){
Object[][] keywords = {
{"xiaomi"},
};
return keywords;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="my">
<classes>
<class name="day03.MyTest3" />
</classes>
</test>
</suite>
6、案例实践
1)思路
在使用TestNG做自动化测试时,基本上大家都会使用DataProvider来管理一个用例的不同测试数据。执行时会发现,测试数据集中的多组数据依然是顺序执行。
解决方式是:在 @DataProvider中添加parallel=true
2)设计脚本
代码如下:
package webtest;
import static org.testng.Assert.assertTrue;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* 使用3个浏览器并发测试搜索
* @author yangzc
*
*/
public class SearchTest2 {
//private WebDriver driver;
@BeforeMethod
public void begin() throws Exception{
//DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
//capability.setBrowserName("internet explorer");
//capability.setPlatform(Platform.WINDOWS);
//driver = new RemoteWebDriver(new URL("http://192.168.0.130:5555/wd/hub"), capability);
//设置浏览器的路径
//System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
//设置浏览器驱动程序的路径
//System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
//System.setProperty("webdriver.gecko.driver", "D:/Python27/geckodriver.exe");
//System.setProperty("webdriver.ie.driver", "D:\\MicrosoftWebDriver.exe");
//创建一个驱动工具(目的是为了操作浏览器),创建驱动工具的时候,浏览器会被打开)
//driver = new ChromeDriver();
//driver = new FirefoxDriver();
//driver = new InternetExplorerDriver();
}
@Test(dataProvider="mydrivers")
public void f(WebDriver driver) throws Exception{
//窗口最大化
driver.manage().window().maximize();
//设置等待时长
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//打开必应搜索网站
driver.get("https://cn.bing.com/");
//点击搜索框
driver.findElement(By.id("sb_form_q")).click();
//清空搜索框里面的内容
driver.findElement(By.id("sb_form_q")).clear();
//输入搜索的关键字
driver.findElement(By.id("sb_form_q")).sendKeys("赵丽颖");
Thread.sleep(3000);
//点击搜索的按钮
driver.findElement(By.id("sb_form_go")).click();
Thread.sleep(3000);
//提交表单sb_form
//driver.findElement(By.id("sb_form")).submit();
//获取HTML页面的源码
String html = driver.getPageSource();
System.out.println(html);
//验证搜索是否成功
assertTrue(html.contains("冯绍峰"));
//
Thread.sleep(3000);
//
driver.quit();
}
@DataProvider(parallel=true)
public Object[][] mydrivers() throws Exception {
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
capability.setBrowserName("internet explorer");
capability.setPlatform(Platform.WINDOWS);
DesiredCapabilities capability2 = DesiredCapabilities.firefox();
capability2.setBrowserName("firefox");
capability2.setPlatform(Platform.WINDOWS);
DesiredCapabilities capability3 = DesiredCapabilities.chrome();
capability3.setBrowserName("chrome");
capability3.setPlatform(Platform.WINDOWS);
Object[][] drivers = {
{new RemoteWebDriver(new URL("http://192.168.0.130:5555/wd/hub"), capability)},
{new RemoteWebDriver(new URL("http://192.168.0.130:5556/wd/hub"), capability2)},
{new RemoteWebDriver(new URL("http://192.168.0.130:5557/wd/hub"), capability3)},
};
return drivers;
}
@AfterMethod
public void end(){
//关闭浏览器
//driver.quit();
}
}
3)运行脚本
参考资料
[01] 简单聊聊TestNG中的并发
https://www.cnblogs.com/znicy/p/6534893.html
微信扫一扫关注该公众号【测试开发者部落】
点击链接加入群聊【软件测试学习交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105