`
darrenzhu
  • 浏览: 804770 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用Maven自动化集成测试

阅读更多
参考文章:http://stackoverflow.com/questions/16935290/maven-deploy-webapp-to-tomcat-before-junit-test


There are a number of schools of thought as to how to handle this type of integration test with Maven.

I should point out that when you are deploying an application to an application server, you are not in the realm of unit testing any more. Because the entire application is being deployed within a container, you are testing the integration of those two components.

Now there is nothing wrong with using JUnit for running integration tests (though there are some limitations that you may hit, for example unit tests should not care about the sequencing of individual tests - assuming you are writing them correctly - so JUnit enforces this by not guaranteeing any sequence of execution... prior to Java 1.7 the execution order was accidentally implied by the order of test methods within a class, but it was not part of the JUnit contract... Some people switch to other testing frameworks for their integration tests, e.g. TestNG, if they find the unit test focus of JUnit is getting in the way of their test development)

The key point to keep in mind is that the Maven lifecycle uses the test phase for the execution of unit tests.

When it comes to integration tests there are two (and a half) schools of thought as to the right way to handle the tests with Maven.

School 1 - Failsafe and integration-test/verify
This school of thought uses the phases after package to start up a container, run the integration tests, tear down the container, and finally check the test results and fail the build in the event of test failures.

NEVER EVER RUN mvn integration-test as that will not tear down the container correctly, any time you think you want to type mvn integration-test you actually want to type mvn verify (oh look, it's shorter and easier to type also... bonus)

So with this you do the following:
Bind tomcat7:run to the pre-integration-test phase with fork=true
Bind failsafe:integration-test to the integration-test phase
Bind tomcat7:shutdown to the post-integration-test phase
Bind failsafe:verify to the verify phase.
For extra brownie points you would use build-helper-maven-plugin:reserve-network-port bound to the validate phase to ensure that the test server is started on an unused network port and then either use resource filtering against the test resources to pass the port through to the tests or use a system property passed through systemPropertyVariables to make the port number available to the tests.

Advantages
Clean Maven build
If the tests fail, you cannot release the project
Can move the integration tests into a separate profile (by convention called run-its) if the tests are too slow to run every build.

Disadvantages
Hard to run the tests from an IDE. All the integration tests start/end in IT and while Maven knows to run tests starting/ending in Test with Surefire and run tests starting/ending in IT with Failsafe, your IDE probably doesn't. Additionally, your IDE is not going to start the container for you, so you have to do a lot of work by hand to actually run the tests manually.
Debugging the tests potentially requires attaching two debuggers, e.g. one to debug the application running in container and the other to debug the test cases.

mvnDebug -Dmaven.failsafe.debug=true verify
Couples your tests to the Maven build process.

School 2 - Separate module
This school of thought moves the integration tests into a separate module that depends on the war module and copies the war into the test resources using, e.g. dependency:copy-dependencies bound to the generate-test-resources phase coupled with a Tomcat7 dependency to test against.

The test cases themselves start up the Tomcat7 container using embedded mode

Advantages
Tests can run in IDE
Integration tests are separated from Unit tests, so asking the IDE to run all tests will not kick off the slower tests

Disadvantages
The war artifact is only rebuilt if you go past the package phase, consequently, you need to run at least mvn clean package periodically to refresh the code under test when using the IDE.
The failure of the integration tests does not break the build of the war module, so you can end up releasing a broken war artifact and then have the reactor build fail for the integration test module. Some people counteract this issue by having the integration test module within src/it and using Maven Invoker Plugin to run the tests... though that provides a poorer IDE integration, so I do not recommend that line.
Hard to get a consolidated test coverage report from Maven.
Have to code the container start/stop yourself from within your test cases.


School 2.5 - Failsafe with the test cases starting their own Tomcat7 server
This is a kind of hybrid of the two approaches.

You use Failsafe to execute the tests, but the tests themselves are responsible for starting and stopping the Tomcat7 container that you want to test in.

Advantages
Don't have to configure the server start/stop in Maven pom
IDE can safely run all tests (though the integration tests may be slower and you may want to not run them, but it's not like they will all fail unless there is a test failure)
Easier to debug the tests from your IDE (only one process to attach against, and the IDE usually makes it easy to debug tests by providing a special test runner)

Disadvantages
Have to code the container start/stop yourself from within your test cases
I hope the above helps you understand the options you have. There may be other tweaks but in general the above are considered the best practice(s) for integration testing with Maven at present.
分享到:
评论

相关推荐

    Jmeter+Jenkins+maven接口自动化集成

    Jmeter+Jenkins+maven 接口自动化集成测试框架 Jmeter+Jenkins+maven 接口自动化集成测试框架是一种自动化测试解决方案,旨在简化测试过程,提高测试效率和测试质量。该框架通过集成 Jmeter、Jenkins 和 Maven 三个...

    Jmeter+Jenkins+maven接口自动化集成.rar

    在这个项目中,我们将利用JMeter、Jenkins和Maven三个强大的工具进行接口自动化集成,实现持续集成和自动化测试。 **JMeter** 是一个开源的性能测试工具,主要适用于Web应用的压力和负载测试,但也可以用于接口测试...

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

    这个实例项目展示了如何将这四个强大的工具集成为一套完整的自动化测试解决方案。 **Java**: Java是一种广泛使用的面向对象的编程语言,以其跨平台的特性闻名,非常适合编写自动化测试脚本。在自动化测试中,Java...

    接口自动化-jenkins+maven+jmeter持续集成.pdf

    文档《接口自动化-jenkins+maven+jmeter持续集成.pdf》详细介绍了如何利用Jenkins、Maven和JMeter这三个强大的工具,搭建一个完整的接口自动化测试和持续集成环境。 首先,文档提到了Jenkins的安装与配置,包括...

    Jenkins_Maven_Git 持续集成及自动化部署 GentOS版

    Jenkins作为CI/CD流水线的核心工具,能够与Maven、Git等工具无缝集成,帮助开发者实现自动化的构建、测试和部署流程。 #### 二、JDK安装 **1. 下载与安装** - **下载**: 访问Oracle官网下载适用于Linux系统的JDK ...

    基于eclipse-maven的自动化测试环境配置大学论文.doc

    在自动化测试环境中,我们可以使用Eclipse和Maven来进行单元测试和集成测试。 3.1 运行单个单元测试 在Eclipse中,我们可以通过right-clicking单元测试类并选择“Run As”->“JUnit Test”来运行单个单元测试。 ...

    hudson+maven+svn自动化部署

    自动化部署的核心在于构建一个稳定且高效的持续集成/持续部署(CI/CD)环境。为了实现这一目标,通常需要以下几项关键技术组件: 1. **JDK (Java Development Kit)**: - **作用**: JDK 是 Java 开发的基础,提供了...

    maven工程在tomcat服务器上的自动化部署

    标题中的“maven工程在tomcat服务器上的自动化部署”指的是使用Maven构建工具与Tomcat应用服务器相结合,实现Java Web项目自动部署的过程。这个过程旨在提高开发效率,减少手动部署的繁琐步骤,使得每次代码更新后都...

    chm版本Maven教程

    Maven自动化部署 Maven Web应用 Eclispe IDE集成Maven NetBeans IDE集成Maven Eclipse构建Maven项目 转换基于Maven的Java项目支持Eclipse IDE 转换基于Maven的Web应用程序支持Eclipse IDE 使用Maven模板创建项目 ...

    springMVC+hibernate maven空框架

    结合 Maven 进行项目管理,可以构建出高效、模块化且易于维护的 Web 应用。 1. **Spring MVC 框架** - **概念**:Spring MVC 是 Spring 框架的一个模块,负责处理 HTTP 请求并呈现响应。它遵循 MVC 设计模式,将...

    Maven和Eclipse集成

    Maven作为一款流行的自动化构建工具,以其强大的依赖管理和项目构建功能而闻名;Eclipse则是广泛使用的开源IDE之一。本书《Maven Integration for Eclipse》由Tim O’Brien、Jason van Zyl等多位专家共同编写,旨在...

    测试maven用到的资源

    在进行持续集成时,Maven可以通过与CI服务器(如Jenkins、GitLab CI/CD)的集成,自动化地执行测试并报告结果。开发者也可以使用maven-site-plugin生成详细的测试报告,包括测试覆盖率、失败测试的堆栈跟踪等信息。 ...

    Jenkins持续集成maven+jmeter+git接口自动化测试框架.pdf

    一个自动化测试框架,可性能也可接口;maven负责项目管理,jenkins服务器就不需要安 装jmeter运行环境(错误的,maven项目配置pom文件),只需要注册脚本所需要的jar包 到本地仓库即可;git源码管理,maven构建; 1...

    持续集成与自动化测试

    【持续集成与自动化测试】 持续集成(Continuous Integration, CI)是一种软件开发实践,它强调开发人员频繁地将他们的代码更改合并到主分支,通常每天至少一次。这一过程伴随着自动化构建和测试,以尽早发现和修复...

    Maven自动升级版本号并打包上传的脚本

    在IT行业中,持续集成和自动化构建是提升效率和质量的关键环节。Maven作为Java项目管理的重要工具,能够帮助我们统一构建、管理和部署项目。本文将详细介绍如何通过编写脚本实现Maven项目的版本号自动升级以及打包...

    许晓斌:Maven与持续集成

    - **自动化构建**:Maven的自动化构建功能能够与持续集成服务器无缝集成,自动执行构建任务。 - **质量保证**:通过持续集成,可以在早期发现并修复缺陷,确保软件质量。 - **快速反馈**:持续集成提供了快速反馈...

    maven产品化支持

    Maven产品化支持是指将Maven用于企业级项目的构建和部署,以实现标准化、自动化和可重复性的过程。通过Maven,开发者可以轻松管理项目的生命周期,构建出符合生产环境需求的可执行包。 【描述】(由于给出的描述为...

    尚硅谷自动化构建工具Maven视频源码课件

    这将帮助你亲手实践Maven的命令行操作,如`mvn clean`, `mvn compile`, `mvn install`等,体验Maven自动下载依赖、编译、测试和打包的过程。同时,你还可以学习到如何解决常见的构建问题,如依赖冲突、插件配置等。 ...

    maven 初始化webapp

    3. **测试**:使用Maven的Surefire插件进行单元测试,Failsafe插件进行集成测试。 4. **打包**:Maven可以将Web应用打包成WAR文件,这是Java EE服务器可部署的格式。 5. **部署**:配置Maven的`deploy`插件,将WAR...

Global site tag (gtag.js) - Google Analytics