- 浏览: 7955033 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
testng笔记4
1 @factory,注解是用来传递不同的参数给测试的,
如果一个测试类的很多方法需要测试,而且这个测试类需要多次测试那么需要使用测试工厂最好。
比如最简单的:
@Test
public void simpleTest(){
System.out.println("Simple Test Method.");
}
@Factory
public Object[] factoryMethod(){
return new Object[]{
new SimpleTest(),
new SimpleTest()
};
}
而
@Factory
public Object[] paramFactory(){
return new Object[]{
new ParameterTest(0),
new ParameterTest(1)
};
}
2 再看一个例子:
public class ParameterFactory {
@Factory
public Object[] paramFactory(){
return new Object[]{
new ParameterTest(0),
new ParameterTest(1)
};
}
}
public class ParameterTest {
private int param;
public ParameterTest(int param){
this.param = param;
}
@Test
public void testMethodOne(){
int opValue=param+1;
System.out.println("Test method one output: "+ opValue);
}
@Test
public void testMethodTwo(){
int opValue=param+2;
System.out.println("Test method two output: "+ opValue);
}
}
最后的运行结果为:
Test method one output: 1
Test method one output: 2
Test method two output: 2
Test method two output: 3
3 @Dataprovider
public class TestParameterDataProvider {
@Test(dataProvider = "provideNumbers")
public void test(int number, int expected) {
Assert.assertEquals(number + 10, expected);
}
@DataProvider(name = "provideNumbers")
public Object[][] provideData() {
return new Object[][] {
{ 10, 20 },
{ 100, 110 },
{ 200, 210 }
};
}
}
输出:
PASSED: test(10, 20)
PASSED: test(100, 110)
PASSED: test(200, 210)
4 @DataProvider + Method
@Test(dataProvider = "dataProvider")
public void test1(int number, int expected) {
Assert.assertEquals(number, expected);
}
@Test(dataProvider = "dataProvider")
public void test2(String email, String expected) {
Assert.assertEquals(email, expected);
}
@DataProvider(name = "dataProvider")
public Object[][] provideData(Method method) {
Object[][] result = null;
if (method.getName().equals("test1")) {
result = new Object[][] {
{ 1, 1 }, { 200, 200 }
};
} else if (method.getName().equals("test2")) {
result = new Object[][] {
{ "test@gmail.com", "test@gmail.com" },
{ "test@yahoo.com", "test@yahoo.com" }
};
}
return result;
}
输出:
PASSED: test1(1, 1)
PASSED: test1(200, 200)
PASSED: test2("test@gmail.com", "test@gmail.com")
PASSED: test2("test@yahoo.com", "test@yahoo.com")
5 并行测试:
下面的例子介绍了如何同时开两个线程来测试:
@Test
public void testMethodsOne() {
long id = Thread.currentThread().getId();
System.out.println("Simple test-method One. Thread id is: "+id);
}
@Test
public void testMethodsTwo() {
long id = Thread.currentThread().getId();
System.out.println("Simple test-method Two. Thread id is: "+id);
}
<suite name="Simple Suite" parallel="methods" thread-count="2" >
<test name="Simple test">
<classes>
<class name="test.parallelism.SimpleClass" />
</classes>
</test>
</suite>
thread-count指定用2个线程来测试
6 并行测试不同的类
比如类一:
public class SampleTestClassOne {
@BeforeClass
public void beforeClass(){
long id = Thread.currentThread().getId();
System.out.println("Before test-class SampleTestClassOne. Thread id is: "+id);
}
@Test
public void testMethodOne() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method One SampleTestClassOne. Thread id is: "+id);
}
@Test
public void testMethodTwo() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method Two SampleTestClassOne. Thread id is: "+id);
}
@AfterClass
public void afterClass(){
long id = Thread.currentThread().getId();
System.out.println("After test-method SampleTestClassOne. Thread id is: "+id);
}
类2 :
<suite name="Test-class Suite" parallel="classes" thread-count="2" >
<test name="Test-class test" group-by-instances="true">
<classes>
<class name="test.parallelism.SampleTestClassOne" />
<class name="test.parallelism.SampleTestClassTwo" />
</classes>
</test>
</suite>
7 同时测试两个test,如
<suite name="Parallel tests" parallel="tests" thread-count="2" >
<test name="Test One">
<parameter name="test-name" value="Test One"/>
<classes>
<class name="test.parallelism.SampleTestSuite" />
</classes>
</test>
<test name="Test Two">
<parameter name="test-name" value="Test Two"/>
<classes>
<class name="test.parallelism.SampleTestSuite" />
</classes>
</test>
</suite>
使用parallel="tests"进行测试
public class SampleTestSuite {
String testName="";
@BeforeTest
@Parameters({ "test-name" })
public void beforeTest(String testName){
this.testName=testName;
long id = Thread.currentThread().getId();
System.out.println("Before test "+testName+". Thread id is: "+id);
}
@BeforeClass
public void beforeClass(){
long id = Thread.currentThread().getId();
System.out.println("Before test-class "+testName+". Thread id is: "+id);
}
@Test
public void testMethodOne() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method "+testName+". Thread id is: "+id);
}
@AfterClass
public void afterClass(){
long id = Thread.currentThread().getId();
System.out.println("After test-method "+testName+". Thread id is: "+id);
}
@AfterTest
public void afterTest(){
long id = Thread.currentThread().getId();
System.out.println("After test "+testName+". Thread id is: "+id);
}
输出:
Before test Test One. Thread id is: 9
Before test Test Two. Thread id is: 10
Before test-class Test One. Thread id is: 9
Before test-class Test Two. Thread id is: 10
Sample test-method Test One. Thread id is: 9
Sample test-method Test Two. Thread id is: 10
After test-method Test One. Thread id is: 9
After test-method Test Two. Thread id is: 10
After test Test One. Thread id is: 9
After test Test Two. Thread id is: 10
8 在多线程中运行单独的测试
public class IndependentTestThreading {
@Test(threadPoolSize=3,invocationCount=6,timeOut=1000)
public void testMethod(){
Long id = Thread.currentThread().getId();
System.out.println("Test method executing on thread with id: "+id);
}
}
其中,threadPoolSize=3表示在三个不同的线程中运行,invocationCount表示执行次数,timeOut表示执行多少毫秒就失败
Test method executing on thread with id: 11
Test method executing on thread with id: 10
Test method executing on thread with id: 9
Test method executing on thread with id: 11
Test method executing on thread with id: 10
Test method executing on thread with id: 9
9 testng maven的配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
其他设置项:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<parallel>tests</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
Have a
1 @factory,注解是用来传递不同的参数给测试的,
如果一个测试类的很多方法需要测试,而且这个测试类需要多次测试那么需要使用测试工厂最好。
比如最简单的:
@Test
public void simpleTest(){
System.out.println("Simple Test Method.");
}
@Factory
public Object[] factoryMethod(){
return new Object[]{
new SimpleTest(),
new SimpleTest()
};
}
而
@Factory
public Object[] paramFactory(){
return new Object[]{
new ParameterTest(0),
new ParameterTest(1)
};
}
2 再看一个例子:
public class ParameterFactory {
@Factory
public Object[] paramFactory(){
return new Object[]{
new ParameterTest(0),
new ParameterTest(1)
};
}
}
public class ParameterTest {
private int param;
public ParameterTest(int param){
this.param = param;
}
@Test
public void testMethodOne(){
int opValue=param+1;
System.out.println("Test method one output: "+ opValue);
}
@Test
public void testMethodTwo(){
int opValue=param+2;
System.out.println("Test method two output: "+ opValue);
}
}
最后的运行结果为:
Test method one output: 1
Test method one output: 2
Test method two output: 2
Test method two output: 3
3 @Dataprovider
public class TestParameterDataProvider {
@Test(dataProvider = "provideNumbers")
public void test(int number, int expected) {
Assert.assertEquals(number + 10, expected);
}
@DataProvider(name = "provideNumbers")
public Object[][] provideData() {
return new Object[][] {
{ 10, 20 },
{ 100, 110 },
{ 200, 210 }
};
}
}
输出:
PASSED: test(10, 20)
PASSED: test(100, 110)
PASSED: test(200, 210)
4 @DataProvider + Method
@Test(dataProvider = "dataProvider")
public void test1(int number, int expected) {
Assert.assertEquals(number, expected);
}
@Test(dataProvider = "dataProvider")
public void test2(String email, String expected) {
Assert.assertEquals(email, expected);
}
@DataProvider(name = "dataProvider")
public Object[][] provideData(Method method) {
Object[][] result = null;
if (method.getName().equals("test1")) {
result = new Object[][] {
{ 1, 1 }, { 200, 200 }
};
} else if (method.getName().equals("test2")) {
result = new Object[][] {
{ "test@gmail.com", "test@gmail.com" },
{ "test@yahoo.com", "test@yahoo.com" }
};
}
return result;
}
输出:
PASSED: test1(1, 1)
PASSED: test1(200, 200)
PASSED: test2("test@gmail.com", "test@gmail.com")
PASSED: test2("test@yahoo.com", "test@yahoo.com")
5 并行测试:
下面的例子介绍了如何同时开两个线程来测试:
@Test
public void testMethodsOne() {
long id = Thread.currentThread().getId();
System.out.println("Simple test-method One. Thread id is: "+id);
}
@Test
public void testMethodsTwo() {
long id = Thread.currentThread().getId();
System.out.println("Simple test-method Two. Thread id is: "+id);
}
<suite name="Simple Suite" parallel="methods" thread-count="2" >
<test name="Simple test">
<classes>
<class name="test.parallelism.SimpleClass" />
</classes>
</test>
</suite>
thread-count指定用2个线程来测试
6 并行测试不同的类
比如类一:
public class SampleTestClassOne {
@BeforeClass
public void beforeClass(){
long id = Thread.currentThread().getId();
System.out.println("Before test-class SampleTestClassOne. Thread id is: "+id);
}
@Test
public void testMethodOne() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method One SampleTestClassOne. Thread id is: "+id);
}
@Test
public void testMethodTwo() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method Two SampleTestClassOne. Thread id is: "+id);
}
@AfterClass
public void afterClass(){
long id = Thread.currentThread().getId();
System.out.println("After test-method SampleTestClassOne. Thread id is: "+id);
}
类2 :
<suite name="Test-class Suite" parallel="classes" thread-count="2" >
<test name="Test-class test" group-by-instances="true">
<classes>
<class name="test.parallelism.SampleTestClassOne" />
<class name="test.parallelism.SampleTestClassTwo" />
</classes>
</test>
</suite>
7 同时测试两个test,如
<suite name="Parallel tests" parallel="tests" thread-count="2" >
<test name="Test One">
<parameter name="test-name" value="Test One"/>
<classes>
<class name="test.parallelism.SampleTestSuite" />
</classes>
</test>
<test name="Test Two">
<parameter name="test-name" value="Test Two"/>
<classes>
<class name="test.parallelism.SampleTestSuite" />
</classes>
</test>
</suite>
使用parallel="tests"进行测试
public class SampleTestSuite {
String testName="";
@BeforeTest
@Parameters({ "test-name" })
public void beforeTest(String testName){
this.testName=testName;
long id = Thread.currentThread().getId();
System.out.println("Before test "+testName+". Thread id is: "+id);
}
@BeforeClass
public void beforeClass(){
long id = Thread.currentThread().getId();
System.out.println("Before test-class "+testName+". Thread id is: "+id);
}
@Test
public void testMethodOne() {
long id = Thread.currentThread().getId();
System.out.println("Sample test-method "+testName+". Thread id is: "+id);
}
@AfterClass
public void afterClass(){
long id = Thread.currentThread().getId();
System.out.println("After test-method "+testName+". Thread id is: "+id);
}
@AfterTest
public void afterTest(){
long id = Thread.currentThread().getId();
System.out.println("After test "+testName+". Thread id is: "+id);
}
输出:
Before test Test One. Thread id is: 9
Before test Test Two. Thread id is: 10
Before test-class Test One. Thread id is: 9
Before test-class Test Two. Thread id is: 10
Sample test-method Test One. Thread id is: 9
Sample test-method Test Two. Thread id is: 10
After test-method Test One. Thread id is: 9
After test-method Test Two. Thread id is: 10
After test Test One. Thread id is: 9
After test Test Two. Thread id is: 10
8 在多线程中运行单独的测试
public class IndependentTestThreading {
@Test(threadPoolSize=3,invocationCount=6,timeOut=1000)
public void testMethod(){
Long id = Thread.currentThread().getId();
System.out.println("Test method executing on thread with id: "+id);
}
}
其中,threadPoolSize=3表示在三个不同的线程中运行,invocationCount表示执行次数,timeOut表示执行多少毫秒就失败
Test method executing on thread with id: 11
Test method executing on thread with id: 10
Test method executing on thread with id: 9
Test method executing on thread with id: 11
Test method executing on thread with id: 10
Test method executing on thread with id: 9
9 testng maven的配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
其他设置项:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<parallel>tests</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
Have a
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1594方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 2024前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3479info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2244import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 499https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 697public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 624https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 676https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 444https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 5531 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3113微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 626https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1849什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 973本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1379原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 870public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 679在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 946-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 777一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1915介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
相关推荐
4. **独立的编译时间与运行时配置**:测试代码与运行时配置分离,允许在不重新编译的情况下改变测试配置。 5. **运行时配置灵活性**:如“测试组”的概念,可以指定运行特定的测试集合,如“快速”、“缓慢”或...
TestNG 是一个强大的自动化测试框架,受到 JUnit 和 NUnit 的启发,但在功能和灵活性上有所增强,特别适合进行单元测试、功能测试、端到端测试和集成测试。它需要 JDK 5 或更高版本来运行。TestNG 的设计目标是为...
### TestNG 学习笔记概览 #### 一、JUnit 的局限性与 TestNG 的优势 ##### JUnit 缺陷概述 - **最初的用途限制**:JUnit 最初被设计为一个仅适用于单元测试的框架,但随着时间的发展,其功能已扩展至支持多种类型...
分成4大模块 第一模块java 基础知识:JDK安装以及环境变量配置、Java 对象和类、public+protected+default+private权限控制等java编程必备知识 第二模块TestNG 自动化测试框架基础:testng 的annotations、TestNG自动...
### Selenium+Eclipse+JUnit+TestNG自动化测试学习笔记 #### 一、环境搭建与配置 **1. 安装 JDK** - **版本**: JDK 1.7 - **下载地址**: ...
4. **MyBatis**:作为持久层框架,MyBatis使得数据库操作更为简单,通过XML或注解进行SQL映射,将SQL语句与Java代码分离,提供灵活的数据访问控制。 5. **MySQL数据库**:项目中可能使用MySQL作为关系型数据库,...
测试是任何软件项目的重要组成部分,可能会使用JUnit或TestNG进行单元测试,确保每个独立的代码模块都能正常工作。而Spring Boot自带的MockMVC可以用来进行模拟HTTP请求的集成测试。 在部署方面,项目可能使用...
- **spring-test**:提供了对 JUnit 或 TestNG 进行集成测试的支持。 - **junit**:一个常用的单元测试框架。 ### 二、数据库访问 #### Spring JDBC 模块 - **spring-jdbc**:提供了对 JDBC 的封装,使得数据访问...
### Java学习笔记要点 #### 一、了解Java ##### 1.1 Java的起源与发展历程 - **起源**: Java 最初是由 Sun 公司在 Green Project 中为了开发 Star7 应用程序而创建的一种编程语言。 - **命名**: 语言的名字来源于 ...
4. **TestNG**:一个流行的测试框架,常与Selenium结合用于自动化测试。 **搭建Selenium+TestNG框架** 1. **创建Java项目**:首先,创建一个新的Java项目,并导入Selenium相关的JAR包,包括selenium-java和selenium...
4. **测试用例设计**:如何编写有效的测试用例,包括正向用例和反向用例,边界值分析,等价类划分,因果图等方法,确保测试覆盖率。 5. **自动化测试**:随着敏捷开发的普及,自动化测试工具如Selenium、JUnit、...
- **测试工具**:JUnit、TestNG 等工具用于进行单元测试和集成测试。 - **项目库**:Maven Central Repository 等仓库提供大量的 Java 项目依赖。 - **论坛与文档**:Stack Overflow、Oracle Java 文档等资源对于...
Eclipse、IntelliJ IDEA等IDE为Java开发提供了强大的集成环境,Maven或Gradle用于项目构建和依赖管理,Junit、TestNG等支持单元测试。 本Java体系笔记覆盖了从基础知识到高级特性的全面内容,帮助读者建立扎实的...
4. **开发工具**:标签中的"工具"可能指的是开发者在Web开发过程中使用的各种辅助软件,如IDE(IntelliJ IDEA、Eclipse)、版本控制系统(Git)、构建工具(Maven、Gradle)、调试器、测试框架(JUnit、TestNG)等。...
4. **等待策略**:由于网页加载异步,Selenium需要等待特定条件满足后再执行下一步操作。你可以使用显式等待(`WebDriverWait`)和隐式等待(`implicitly_wait`)来确保元素的可操作性。 5. **测试框架集成**:...
### Java+JDK6学习笔记知识点详解 #### 一、Java简介 - **起源与历史:** - 最初由Sun公司的Green Project发起,旨在创建一个名为Star7的应用程序编程语言。 - 名称来源于创始人James Gosling窗外的一棵橡树...
4. 页面对象模型(POM):这是一种设计模式,将页面元素和相关操作封装,降低脚本维护成本,提高代码复用性。 5. 自动化测试套件:组织和管理多个测试用例,便于执行整个测试流程,例如JUnit或TestNG。 6. 报告和...
- **Test**:支持测试Spring应用,包括JUnit和TestNG集成。 2. **Spring框架的搭建** 搭建Spring环境首先需要引入必要的依赖库,例如:aopalliance、commons-logging、spring-aop、spring-beans、spring-context...
同时,IDEA也支持JUnit和TestNG等单元测试框架,方便进行测试编写和运行。 9. **插件安装**: IDEA的扩展性极强,可以在"File" -> "Settings" -> "Plugins"中搜索并安装各种插件,以满足特定需求,如Lombok插件、...
4. **异常处理**:Java的异常处理机制是其强大的错误处理工具,通过try-catch-finally和throws关键字,能够优雅地处理运行时错误。 5. **数组和集合框架**:数组是存储同类型数据的基础结构,而集合框架(如List、...