如何在maven项目上分离单元测试和集成测试

发布于 2024-12-11 04:11:41 字数 153 浏览 0 评论 0原文

我有一个 Maven 项目,里面有很多 junit 类。我用 Eclipse 开发。我想将功能测试类和集成测试类分开。

当我在 Eclipse 中构建项目时,我只想执行功能测试类。

按照詹金斯的说法,他们两个都应该被处决。

我应该遵循哪种方法?

I have a maven project and lots of junit classes in it. I develop with Eclipse. I want to separate functional test classes and integration testing classes.

When I build the project in Eclipse then I want only the functional test classes to be executed.

By jenkins both of them should be executed.

Which approach should i follow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉鱼一梦 2024-12-18 04:11:41

我发现将集成测试放在单独的项目中更方便,然后依靠 Maven 的默认生命周期像单元测试一样运行它们。由于我必须针对不同的环境运行测试,因此这种方法可以更轻松地管理特定于环境的测试。

假设我有一个应用程序,由 application Maven 聚合器项目表示,其中包含一个名为 project 的 jar 模块。我将单元测试保留在 project 本身中,以便每当我构建应用程序时都会执行它们。这也是 Jenkins 每天晚上构建的;理想情况下,成功的构建应该自动部署到一个或多个测试环境,以进行手动和自动测试。目前这是手工完成的。

对于需要运行集成测试的每个环境,我都有一个 applicationTestEnvX Maven 聚合器项目。它至少包含一个 projectTest 模块,我在其中保存那些独立于环境的集成测试以及任何测试支持代码。特定于环境 X 的 project 模块的测试保存在 projectTestEnvX 模块中。我为每个 applicationTestEnvX 项目都有一个 Jenkins 作业,它每晚运行我的测试。理想情况下,这些应该针对应用程序构建的结果运行,但我还没有做到这一点。

与我的项目在 Subversion 和 Eclipse 工作区中的存储方式也有直接对应,但那是另一个故事了;-)

I find it more convenient to put integration tests in separate projects, and then run them as if they were unit tests by relying on Maven's default life cycle. As I have to run my tests against different environments, this approach makes it easier to manage environment specific tests.

Let's assume I have an application, represented by the application Maven aggregator project, which contains a jar module called project. I keep unit tests within project itself, so that they get executed whenever I build my application. This is also built every night by Jenkins; ideally successful builds should be automatically deployed to one or more test environments, for both manual and automatic tests. Currently this is done by hand.

For every environment where I need to run my integration tests I have an applicationTestEnvX Maven aggregator project. This contains at least a projectTest module, where I keep those integration tests that are environment independent, as well as any test support code. Tests for my project module that are specific to environment X are kept in a projectTestEnvX module. I have a Jenkins job for each applicationTestEnvX project, which runs my tests every night. Ideally these should be run against the result of the application build, but I'm not there yet.

There is also a direct correspondence with how my projects are stored in Subversion and my Eclipse workspaces, but that's another story ;-)

话少心凉 2024-12-18 04:11:41

看一下两个 Maven 插件:Surefire(用于单元测试)和 Failsafe(用于集成测试)。它们彼此非常相似,Failsafe 是 Surefire 的克隆。

组织您的测试,使其命名架构符合建议的配置:**/*Test.java 用于单元测试,**/*IT.java 用于集成。 Surefire 默认运行,对于 Failsafe,您需要 POM 中的额外摘录 — 示例和更多信息 在此答案中

然后就是 mvn testmvnintegration-test

如果您只想在某些环境(Jenkins)中运行集成测试,您可以使 Failsafe 仅在配置文件中执行,例如:

<profiles>
    <profile>
        <id>env-itest</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                 <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals><goal>integration-test</goal></goals>
                    </execution>
                    <!-- other executions, if needed -->
                 </executions>
            </plugin>
        </plugins>
    </profile>
</profiles>

然后,在 Jenkins 上运行 mvn clean install -P env-itest并且在您的本地环境中仅使用mvn clean install(或类似)。

Have a look at two Maven plugins: Surefire (for unit tests) and Failsafe (for integration tests). They closely resemble each other, Failsafe is a clone of Surefire.

Organize your tests so that their naming schema goes with proposed configuration: **/*Test.java for unit tests and **/*IT.java for integration. Surefire is run by default, for Failsafe you'll need extra excerpt in POM — example and more info in this answer.

Then it's down to mvn test or mvn integration-test.

If you want to run integration test only in certain environments (Jenkins), you could make Failsafe executing only in a profile, for example:

<profiles>
    <profile>
        <id>env-itest</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                 <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals><goal>integration-test</goal></goals>
                    </execution>
                    <!-- other executions, if needed -->
                 </executions>
            </plugin>
        </plugins>
    </profile>
</profiles>

Then, on Jenkins, you run mvn clean install -P env-itest and on your local environment only mvn clean install (or simliar).

-小熊_ 2024-12-18 04:11:41

查看有关集成测试阶段的 Maven 文档 - 您可以使用不同的插件来控制运行哪些测试,只需适当地命名测试即可。

然后运行 ​​mvn test 将构建 &运行你的代码&单元测试, mvn verify 也将运行您的集成测试。

Have a look at the maven documentation around the integration test phase - you can use different plugins to control which tests are run, simply by naming the tests appropriately.

Then running mvn test will build & run your code & unit tests, which mvn verify will run your integration tests as well.

屋檐 2024-12-18 04:11:41

您可以将单元测试和集成测试分离到单独的包中(甚至可能是单独的源文件夹,但随后您必须更新 Maven 配置以识别您有两个单独的源文件夹用于测试)。

要利用这一点,在 Eclipse 的运行配置 (Run > Run Configurations) 中,创建一个新的 JUnit 运行配置,“运行所选项目、包或源文件夹:”,选择仅包含您要运行的测试的包/源文件夹。


当我第一次读到你的问题时,我把它搞反了。我以为您想在 Eclipse 中运行完整套件,而只在 Jenkins 中运行一个子集。我将保留我的旧答案,以防您发现这在某种程度上有用:

我之前完成此操作的方式是通过 JUnit 测试用例的命名约定。

我会将所有单元测试测试用例命名为 ...UnitTest (例如 RegistrationManagerUnitTest),并将集成测试测试用例命名为 ...IntegrationTest< /code>(例如,RegistrationDaoIntegrationTest)。

然后在 Maven 中,您可以将其配置为运行类以 ...UnitTest 结尾的所有测试用例(默认情况下,它会查找名称以 ...Test 结尾的类>. 大致如下:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- Run only tests whose name end with "UnitTest" -->
        <includes>
            <include>**/*UnitTest.java</include>
        </includes>
    </configuration>
</plugin>

You can separate your unit and integration tests into separate packages (or perhaps even separate source folders, but then you'd have to updated your Maven configuration to recognize that you have two separate source folders for tests).

To take advantage of this, in Eclipse's Run Configurations (Run > Run Configurations), create a new JUnit run configuration that "Run all tests in the selected project, package or source folder:", select the the package/source folder containing only the tests you want to run.


When I first read your question, I got it backwards. I thought you wanted to run the full suite in Eclipse, and only a subset in Jenkins. I'm going to just leave my old answer up in case you find this useful some how:

The way I've done this before is through naming convention of the JUnit Test Cases.

I would name all the unit test test cases ...UnitTest (e.g., RegistrationManagerUnitTest) and integration test test cases, I'd name ...IntegrationTest (e.g., RegistrationDaoIntegrationTest).

Then in Maven, you can configure it to run all the test cases whose classes end with ...UnitTest (by default it's looking for classes whose name end with ...Test. Something along the lines of:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <!-- Run only tests whose name end with "UnitTest" -->
        <includes>
            <include>**/*UnitTest.java</include>
        </includes>
    </configuration>
</plugin>
独自←快乐 2024-12-18 04:11:41

仅供参考,使用 TestNG,您只需使用组,例如 @Test(groups = "integration") 和 @Test(groups = “单元”)。然后,您只需根据需要运行不同的组即可。

FYI, with TestNG, you would simply use groups, e.g. @Test(groups = "integration") and @Test(groups = "unit"). Then you simply run different groups depending on what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文