`

maven2 TestNG

阅读更多

TestNG 在maven2 的pom.xml文件里的配置:

Configuring TestNG

To get started with TestNG, include the following dependency in your project:

[...]
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>5.8</version>
    <scope>test</scope>
    <classifier>jdk15</classifier>
  </dependency>
[...]

Note: if you are using JDK 1.4 Javadoc annotations for your TestNG tests, replace jdk15 with jdk14 above.

This is the only step that is required to get started - you can now create tests in your test source directory (eg, src/test/java , and as long as they are named using the defaults such as *Test.java they will be run by Surefire as TestNG tests.

If you'd like to use a different naming scheme, you can change the includes parameter, as discussed in the Inclusions and Exclusions of Tests example.

Using Suite XML Files

Another alternative is to use TestNG suite XML files. This allows flexible configuration of the tests to be run. These files are created as normal, and then added to the Surefire Plugin configuration:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
      </suiteXmlFiles>
    </configuration>
  </plugin>
[...]

This configuration will override the includes and excludes patterns and run all tests in the suite files.

Specifying Test Parameters

Your TestNG test can accept parameters with the @Parameters annotation. You can also pass parameters from Maven into your TestNG test, by specifying them as system properties, like this:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <systemProperties>
        <property>
          <name>browser</name>
          <value>firefox</value>
        </property>
      </systemProperties>
    </configuration>
  </plugin>
[...]

For more information about setting system properties in Surefire tests, see System Properties .

Using Groups

TestNG allows you to group your tests. You can then execute a specific group or groups. To do this with Surefire, use the groups parameter, for example:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <groups>functest,perftest</groups>
    </configuration>
  </plugin>
[...]

Likewise, the excludedGroups parameter can be used to run all but a certain set of groups.

Running tests in parallel

TestNG allows you to run your tests in parallel, including JUnit tests. To do this, you must set the parallel parameter, and may change the threadCount parameter if the default of 5 is not sufficient. For example:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <parallel>methods</parallel>
      <threadCount>10</threadCount>
    </configuration>
  </plugin>
[...]

This is particularly useful for slow tests that can have high concurrency, or to quickly and roughly assess the independance and thread safety of your tests and code.

Using custom listeners and reporters

TestNG provides support for attaching custom listeners, reporters, annotation transformers and method interceptors to your tests. By default, TestNG attaches a few basic listeners to generate HTML and XML reports.

You can configure multiple custom listeners like this:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <properties>
        <property>
          <name>usedefaultlisteners</name>
          <value>false</value> <!-- disabling default listeners is optional -->
        </property>
        <property>
          <name>listener</name>
          <value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value>
        </property>
        <property>
          <name>reporter</name>
          <value>listenReport.Reporter</value>
        </property>
      </properties>
    </configuration>
  </plugin>
[...]
分享到:
评论

相关推荐

    java+selenium+maven+testng自动化测试框架实例(实际项目)

    Java+Selenium+Maven+TestNG自动化测试框架是现代软件开发中的一个重要组成部分,尤其是在Web应用程序的质量保证阶段。这个实例项目展示了如何将这四个强大的工具集成为一套完整的自动化测试解决方案。 **Java**: ...

    maven+testng+extenreport项目

    2. **TestNG测试编写**:在项目中创建测试类,使用TestNG的注解(如@Test, @BeforeSuite, @AfterSuite等)来定义测试用例和前置/后置条件。TestNG支持数据驱动测试,可以通过XML文件或者注解方式提供测试数据。 3. ...

    TestNG-with-Jacoco:使用 Jacoco 和 Maven 进行 TestNG

    使用 Jacoco 和 Maven 进行 TestNG 创建文件夹 D:\Embedded_Tomcat_Temp 并将 cgw war 复制到其中。 使用 mvn install 构建战争并将其复制到 D:\Embedded_Tomcat_Temp run mvn "clean package" for testng + ...

    全网最全Selenium3+Java+Maven+TestNG+ReportNG+SVN+Jenkins自动化测试环境搭建

    ### Selenium3+Java+Maven+TestNG+ReportNG+SVN+Jenkins自动化测试环境搭建 #### Java环境搭建 **一、安装JDK1.8** - **步骤**: 下载JDK安装包并按照向导指引进行安装,直至安装完成。 **二、配置JDK环境变量** ...

    Java+Maven+TestNG参考案例

    本篇将详细讲解如何使用Java、Maven以及TestNG来构建一个接口自动化测试框架。 首先,让我们了解这三者的核心功能: 1. Java:作为一种强大的面向对象编程语言,Java广泛应用于后端开发和自动化测试。其跨平台的...

    java+idea+maven+testng环境搭建

    Java+IntelliJ IDEA+Maven+TestNG 环境搭建是自动化测试的重要步骤,主要涉及以下几个核心知识点: 1. **IntelliJ IDEA**: IntelliJ IDEA 是一款强大的 Java 开发集成环境,支持多种编程语言,提供了丰富的代码补全...

    testng.rar_maven testng_testng_testng.xml.html

    在这个"testng.rar"压缩包中,包含的是一个使用Maven构建系统,并结合TestNG进行测试的项目。 首先,让我们来深入了解一下TestNG。TestNG由Cedric Beust创建,它的设计灵感来源于JUnit,但引入了许多新的特性,如...

    selenium3+java+Maven+TestNG+ReportNG+Excel自动化参数测试框架源码

    这是一个基于Selenium 3、Java、Maven、TestNG、ReportNG和Excel的自动化测试框架。这个框架结合了多种强大的工具和技术,旨在提高软件测试的效率和准确性,尤其是在Web应用程序的自动化测试领域。 1. **Selenium 3...

    selenium+java+Maven-TestNG+reportNG框架代码

    #框架介绍 selenium+java+Maven-TestNG+reportNG+XML • 使用Java作为项目编程语言 • 使用Maven作为项目类型,方便管理架包 • 使用TestNG作为项目运行框架,方便执行测试用例,生成测试报告 • 使用Xml作为...

    eclipsemaven-testng-svn环境配置[整理].pdf

    【Eclipse Maven TestNG SVN环境配置详解】 在软件开发中,高效的工作环境是提升生产力的关键。本篇将详细介绍如何配置Eclipse、Maven、TestNG和SVN的环境,以确保顺利进行Java项目的开发和协作。 1. **安装...

    eclipse下Selenium2.0+-Maven+TestNG环境搭建

    ### Eclipse 下 Selenium 2.0+ Maven + TestNG 环境搭建详解 #### 前言 在软件测试领域,尤其是Web应用的功能自动化测试中,Selenium 是一个非常流行的框架。结合Maven(用于构建管理和依赖管理)与 TestNG(用于...

    Java_page_object_maven_testNG:Java_page_object_maven_testNG

    2. **src/test/java**: 存放TestNG测试类,这些类将引用页面对象并执行测试。 3. **pom.xml**: Maven配置文件,定义了项目依赖、构建过程等信息。 4. **testng.xml**: TestNG的配置文件,用于指定要执行的测试套件和...

    maven testng 报告中文乱码(csdn)————程序.pdf

    2. **TestNG的编码设置**:在`pom.xml`中,你需要配置TestNG插件以使用UTF-8编码生成报告。可以通过添加以下配置到`maven-surefire-plugin`或`maven-failsafe-plugin`(如果你的测试在`@IntegrationTest`注解下)来...

    selenide-maven-testng-java

    2. **Maven**:Maven是一个强大的项目管理和构建工具,使用XML格式的POM(Project Object Model)文件来定义项目的构建过程、依赖关系和项目信息。通过Maven,开发者可以轻松地执行编译、测试、打包、部署等任务。在...

    Tatoc_Maven:使用Maven,TestNG的Tatoc自动化代码

    【Tatoc_Maven: Maven与TestNG在自动化测试中的应用】 Tatoc_Maven项目是一个专注于使用Maven构建工具和TestNG测试框架进行自动化测试的示例。在这个项目中,我们将深入探讨如何利用这两项技术来提升Java应用程序的...

    jenkins+testng+reportng+maven+java环境搭建

    jenkins+testng+reportng+maven+java环境搭建 本文将指导测试人员搭建jenkins+testng+reportng+maven+java环境,涵盖了Eclipse环境准备、Maven配置、TestNG插件安装、ReportNG报告引入等内容。 一、Eclipse 环境...

Global site tag (gtag.js) - Google Analytics