`

testng笔记4

 
阅读更多
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
0
分享到:
评论

相关推荐

    TestNG笔记

    4. **独立的编译时间与运行时配置**:测试代码与运行时配置分离,允许在不重新编译的情况下改变测试配置。 5. **运行时配置灵活性**:如“测试组”的概念,可以指定运行特定的测试集合,如“快速”、“缓慢”或...

    TestNG中文手册学习笔记

    TestNG 是一个强大的自动化测试框架,受到 JUnit 和 NUnit 的启发,但在功能和灵活性上有所增强,特别适合进行单元测试、功能测试、端到端测试和集成测试。它需要 JDK 5 或更高版本来运行。TestNG 的设计目标是为...

    TestNG学习笔记

    ### TestNG 学习笔记概览 #### 一、JUnit 的局限性与 TestNG 的优势 ##### JUnit 缺陷概述 - **最初的用途限制**:JUnit 最初被设计为一个仅适用于单元测试的框架,但随着时间的发展,其功能已扩展至支持多种类型...

    【TestNG自动化测试框架】TestNG自动化测试框架入门到实战完整笔记

    分成4大模块 第一模块java 基础知识:JDK安装以及环境变量配置、Java 对象和类、public+protected+default+private权限控制等java编程必备知识 第二模块TestNG 自动化测试框架基础:testng 的annotations、TestNG自动...

    Selenium+Eclipse+Junit+TestNG自动化学习笔记

    ### Selenium+Eclipse+JUnit+TestNG自动化测试学习笔记 #### 一、环境搭建与配置 **1. 安装 JDK** - **版本**: JDK 1.7 - **下载地址**: ...

    达内Java项目云笔记12天完整源码cloudnote_day12_all.zip

    4. **MyBatis**:作为持久层框架,MyBatis使得数据库操作更为简单,通过XML或注解进行SQL映射,将SQL语句与Java代码分离,提供灵活的数据访问控制。 5. **MySQL数据库**:项目中可能使用MySQL作为关系型数据库,...

    云笔记资源代码

    测试是任何软件项目的重要组成部分,可能会使用JUnit或TestNG进行单元测试,确保每个独立的代码模块都能正常工作。而Spring Boot自带的MockMVC可以用来进行模拟HTTP请求的集成测试。 在部署方面,项目可能使用...

    ssh学习笔记

    - **spring-test**:提供了对 JUnit 或 TestNG 进行集成测试的支持。 - **junit**:一个常用的单元测试框架。 ### 二、数据库访问 #### Spring JDBC 模块 - **spring-jdbc**:提供了对 JDBC 的封装,使得数据访问...

    java学习笔记 良格格

    ### Java学习笔记要点 #### 一、了解Java ##### 1.1 Java的起源与发展历程 - **起源**: Java 最初是由 Sun 公司在 Green Project 中为了开发 Star7 应用程序而创建的一种编程语言。 - **命名**: 语言的名字来源于 ...

    Selenium WebDriver的笔记整理

    4. **TestNG**:一个流行的测试框架,常与Selenium结合用于自动化测试。 **搭建Selenium+TestNG框架** 1. **创建Java项目**:首先,创建一个新的Java项目,并导入Selenium相关的JAR包,包括selenium-java和selenium...

    软件测试内部教程笔记

    4. **测试用例设计**:如何编写有效的测试用例,包括正向用例和反向用例,边界值分析,等价类划分,因果图等方法,确保测试覆盖率。 5. **自动化测试**:随着敏捷开发的普及,自动化测试工具如Selenium、JUnit、...

    Java+JDK+6学习笔记.pdf

    - **测试工具**:JUnit、TestNG 等工具用于进行单元测试和集成测试。 - **项目库**:Maven Central Repository 等仓库提供大量的 Java 项目依赖。 - **论坛与文档**:Stack Overflow、Oracle Java 文档等资源对于...

    java体系笔记

    Eclipse、IntelliJ IDEA等IDE为Java开发提供了强大的集成环境,Maven或Gradle用于项目构建和依赖管理,Junit、TestNG等支持单元测试。 本Java体系笔记覆盖了从基础知识到高级特性的全面内容,帮助读者建立扎实的...

    web笔记两连发

    4. **开发工具**:标签中的"工具"可能指的是开发者在Web开发过程中使用的各种辅助软件,如IDE(IntelliJ IDEA、Eclipse)、版本控制系统(Git)、构建工具(Maven、Gradle)、调试器、测试框架(JUnit、TestNG)等。...

    Selenium学习笔记源代码

    4. **等待策略**:由于网页加载异步,Selenium需要等待特定条件满足后再执行下一步操作。你可以使用显式等待(`WebDriverWait`)和隐式等待(`implicitly_wait`)来确保元素的可操作性。 5. **测试框架集成**:...

    Java+JDK6学习笔记(PDF版书籍,免费下载)

    ### Java+JDK6学习笔记知识点详解 #### 一、Java简介 - **起源与历史:** - 最初由Sun公司的Green Project发起,旨在创建一个名为Star7的应用程序编程语言。 - 名称来源于创始人James Gosling窗外的一棵橡树...

    HSAE自动化工具,随手笔记

    4. 页面对象模型(POM):这是一种设计模式,将页面元素和相关操作封装,降低脚本维护成本,提高代码复用性。 5. 自动化测试套件:组织和管理多个测试用例,便于执行整个测试流程,例如JUnit或TestNG。 6. 报告和...

    Spring学习总结笔记

    - **Test**:支持测试Spring应用,包括JUnit和TestNG集成。 2. **Spring框架的搭建** 搭建Spring环境首先需要引入必要的依赖库,例如:aopalliance、commons-logging、spring-aop、spring-beans、spring-context...

    IDEA快速使用入门笔记.zip

    同时,IDEA也支持JUnit和TestNG等单元测试框架,方便进行测试编写和运行。 9. **插件安装**: IDEA的扩展性极强,可以在"File" -&gt; "Settings" -&gt; "Plugins"中搜索并安装各种插件,以满足特定需求,如Lombok插件、...

    java学习笔记

    4. **异常处理**:Java的异常处理机制是其强大的错误处理工具,通过try-catch-finally和throws关键字,能够优雅地处理运行时错误。 5. **数组和集合框架**:数组是存储同类型数据的基础结构,而集合框架(如List、...

Global site tag (gtag.js) - Google Analytics