在本教程中,我们将向您展示如何使用@Test属性invocationCount
和threadPoolSize
在网站上执行负载测试或压力测试。
使用的工具 :
- 测试NG 6.8.7
- 硒2.39.0
- Maven 3
PS:我们正在使用Selenium库来使浏览器自动访问网站。
1.项目依赖
获取TestNG和Selenium库。
<properties>
<testng.version>6.8.7</testng.version>
<selenium.version>2.39.0</selenium.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependencies>
2. @Test(invocationCount =?)
此invocationCount
确定了TestNG应该运行此测试方法多少次。
范例2.1
@Test(invocationCount = 10)
public void repeatThis() {
//...
}
输出– repeatThis()
方法将运行10次。
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
示例2.2 –使用Selenium打开Firefox浏览器并加载“ Google.com”。 此测试是为了确保页面标题始终为“ Google”。
package com.mkyong.testng.examples.loadtest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestMultipleThreads {
@Test(invocationCount = 5)
public void loadTestThisWebsite() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("Page Title is " + driver.getTitle());
Assert.assertEquals("Google", driver.getTitle());
driver.quit();
}
}
输出–您会注意到Firefox浏览器将提示并关闭5次。
Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
3. @Test(invocationCount =?,threadPoolSize =?)
threadPoolSize
属性告诉TestNG创建线程池以通过多个线程运行测试方法。 使用线程池,将大大减少测试方法的运行时间。
例3.1 –启动一个包含3个线程的线程池,并运行3次测试方法。
@Test(invocationCount = 3, threadPoolSize = 3)
public void testThreadPools() {
System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
}
输出–测试方法运行3次,每个方法都有自己的线程。
[ThreadUtil] Starting executor timeOut:0ms workers:3 threadPoolSize:3
Thread Id : 10
Thread Id : 12
Thread Id : 11
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
示例3.2 –启动一个包含3个线程的线程池,并运行10次测试方法。
@Test(invocationCount = 10, threadPoolSize = 3)
public void testThreadPools() {
System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
}
输出–测试方法运行10次,并且线程被重用。
[ThreadUtil] Starting executor timeOut:0ms workers:10 threadPoolSize:3
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
4.负载测试示例
通过结合TestNG多线程和功能强大的Selenium浏览器自动化。 您可以创建一个简单而强大的负载测试,如下所示:
package com.mkyong.testng.examples.loadtest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestMultipleThreads {
@Test(invocationCount = 100, threadPoolSize = 5)
public void loadTest() {
System.out.printf("%n[START] Thread Id : %s is started!",
Thread.currentThread().getId());
WebDriver driver = new FirefoxDriver();
driver.get("http://yourwebsite.com");
//perform whatever actions, like login, submit form or navigation
System.out.printf("%n[END] Thread Id : %s",
Thread.currentThread().getId());
driver.quit();
}
}
输出–上面的测试将启动5个线程池,并将100个URL请求发送到指定的网站。
[ThreadUtil] Starting executor timeOut:0ms workers:100 threadPoolSize:5
[START] Thread Id : 11 is started!
[START] Thread Id : 14 is started!
[START] Thread Id : 10 is started!
[START] Thread Id : 12 is started!
[START] Thread Id : 13 is started!
[END] Thread Id : 11
[START] Thread Id : 11 is started!
[END] Thread Id : 10
[START] Thread Id : 10 is started!
[END] Thread Id : 13
[START] Thread Id : 13 is started!
[END] Thread Id : 14
[START] Thread Id : 14 is started!
[END] Thread Id : 12
[START] Thread Id : 12 is started!
[END] Thread Id : 13
......
常见问题
问:对于使用Selenium和TestNG进行负载测试,为什么一次只提示一个浏览器?
答:您的测试方法完成得太快,请尝试放置Thread.sleep(5000)
来延迟执行,现在,您应该注意到多个浏览器同时提示。 (仅用于演示目的)。
下载源代码
下载– TestNG-LoadTest-Example.zip (15 kb)
参考文献
翻译自: https://mkyong.com/unittest/testng-selenium-load-testing-example/