Project将测试与src分开

Project将测试与src分开

本文介绍了使用Maven Project将测试与src分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始从事Java项目(因为我通常使用.net),而让我感到奇怪的第一件事就是在Maven项目中有一个/src和/test目录,其中显然,源代码和测试应该进行.

I've just started working on a Java project (as I usually use .net), and one of the first things that strikes me as odd is that in the Maven project there is a /src and a /test directory where obviously the source code and the tests should go.

在.net中,我更喜欢将测试放在单独的程序集/项目中,因此,例如,我将拥有:

In .net I preferred to have the tests in a separate assembly/project, so for example I would have:

  • MyProject
  • MyProject.Tests

这样,我不必使用任何测试来膨胀部署的代码,并且可以更轻松地以真正的隔离方式测试我的代码,并且在很多情况下,我不必为每个项目编写测试,我将拥有广泛的解决方案单元/集成/验收测试,即MySolution.UnitTests,MySolution.IntegrationTests.

That way I dont have to bloat my deployed code with any tests and it makes it easier to test my code in true isolation and in a lot of cases I didn't bother writing tests per project, I would just have solution wide unit/integration/acceptance tests i.e MySolution.UnitTests, MySolution.IntegrationTests.

但是在Java中,它似乎是捆绑在一起的,我宁愿将其分开,但是我听说当您想要以不同的方式处理默认结构时,Maven是一个残酷的情妇.

However in Java it just seems to be bundled together, and I would rather separate it out, however I hear that Maven is a cruel mistress when you want to do things differently to the default structures.

所以要重新整理这篇帖子,我的主要问题是:

So to reign this post back in, my main questions are:

  • 有没有办法将测试与项目分开
  • 基于上述信息,在项目中实际进行测试是否有任何优势? (除了您进行检查时,总会在那里进行测试)

推荐答案

可部署的工件(jar文件,war文件)将不包含测试类或数据.

The deployable artifacts (jar file, war files) will not contain the test classes or data.

您可以将其分为两个项目,一个"Test"项目仅包含测试,并取决于"real"项目.

You could split it into two projects, with a "Test" project containing only the tests, and depending on the "real" project.

但是,尤其是对于Maven,您可能想要遵循其他所有人(或至少大多数人)都遵循的相同的项目布局约定.这将使您的生活更轻松(更少的配置).

However, especially with Maven, you probably want to follow the same project layout conventions that everyone else (or at least the majority) has. This will make your life easier (less configuration).

同样,测试不会将其放入要部署的产品中,因此当前的布局应该不会有问题.

Again, the tests will not make it into the product to be deployed, so the current layout should not be a problem.

对于集成测试,这实际上是有道理的.在这种情况下,定义一个测试"项目,该项目依赖于构成解决方案的所有其他项目(我仍然将单元测试与他们测试的项目保持在一起).

For integration tests, that actually makes sense. In this case, define a "Test" project that depends on all the other projects that make up the solution (I'd still keep the unit tests with the project they test).

这篇关于使用Maven Project将测试与src分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:27