简体   繁体   English

如何从其他Maven项目运行Maven项目的测试

[英]How to run tests of maven project from a different maven project

I have two maven projects say project1 and project2. 我有两个Maven项目,分别是project1和project2。 project2 is added in the project1 dependencies due to some functional cause. 由于某些功能原因,将project2添加到project1依赖项中。

I have a unit test in project1, it is running fine when i do clean install for project1. 我在project1中进行了单元测试,当我为project1进行全新安装时,它运行良好。

My requirement is, the tests in the project1 should execute when i run the project2. 我的要求是,当我运行project2时,应该执行project1中的测试。 If the above is not possible, atleast do a clean install for project1 when i run the project2. 如果上述方法不可行,则在我运行project2时至少要对project1进行全新安装。

I cannot add project1 in the project2 dependencies, because it causes cyclic dependencies. 我无法在project2依赖项中添加project1,因为它会导致循环依赖项。 Also i cannot add a common parent to these two projects, due to some other reasons. 另外,由于其他一些原因,我无法在这两个项目中添加共同的父母。

Is there any way to do this? 有什么办法吗? I would really appreciate if anyone can help me in this. 如果有人可以帮助我,我将不胜感激。

Here is the real situation: 这是真实情况:

project1 has a data xml say project1_xml and project2 has a data xml say project2_xml. project1的数据xml表示project1_xml,而project2的数据xml表示project2_xml。

We need to make sure a specific part of data in both the xmls are same, if anyone comes and changes the data in only one xml and builds that project then they should see error. 我们需要确保两个xml中数据的特定部分相同,如果有人来并且仅更改一个xml中的数据并构建该项目,那么他们应该会看到错误。

So i wrote some code in project1 to parse both the xmls and return map objects. 所以我在project1中编写了一些代码来解析xml和返回映射对象。 And also a unit test in project1 gets the map objects and do some comparison and throws the results. 并且project1中的单元测试还获取地图对象并进行一些比较并抛出结果。

Why? 为什么? Project 2 is a dependency of Project 1, but Project 2 does not and should not have any idea as to whose dependency it is! 项目2是项目1的依赖项,但是项目2没有也不应该知道它是谁的依赖项!

Let's look at it this way. 让我们这样看。 You have a project (Project 1). 您有一个项目(项目1)。 As a dependency to your project, you add Hibernate (Project 2). 作为对项目的依赖项,您添加了Hibernate(项目2)。 Now when someone runs tests in Hibernate, would you expect it to run tests in your personal projects as well? 现在,当有人在Hibernate中运行测试时,您是否希望它也在您的个人项目中运行测试?

Let's say later someone creates Project 3 and then adds Project 2 as a dependency. 假设以后有人创建Project 3,然后将Project 2添加为依赖项。 Should Project 2 now run Project 3's tests as well? Project 2现在是否也应该运行Project 3的测试?

Doesn't make sense, right? 没道理吧? :) You have to model your dependencies appropriately. :)您必须适当地对依赖关系进行建模。

Also if you think about it, you shouldn't even have to run your dependencies' tests. 同样,如果您考虑一下,您甚至不必运行依赖项的测试。 When you pull in a dependency, you are pulling in a self-contained artifact. 引入依赖项时,就引入了一个独立的工件。 One that you hope, has been tested before being pushed out. 您希望的一种在推出之前已经过测试。 Those dependencies' tests would be run during their continuous integration and not during yours. 这些依赖项的测试将在它们的持续集成过程中运行,而不是在您的集成过程中运行。 Can you imagine running all the unit tests of all your dependencies in your project? 您能想象在项目中运行所有依赖项的所有单元测试吗? It would take a very long time, right? 需要很长时间,对不对? So I would urge you to rethink the relationships between your dependencies. 因此,我敦促您重新考虑依赖关系之间的关系。

If you really think that Project 1's tests should run in Project 2, and there is no way around it at all, then take a look at the code as well. 如果您真的认为Project 1的测试应该在Project 2中运行,并且根本无法解决它,那么也请看一下代码。 Perhaps it means that both of these projects should be under different modules in a single project. 也许这意味着这两个项目都应该在一个项目中处于不同的模块下。

Hum it sounds complicated, first question: if these tests are truly unit testing, why can't you add these tests to project2 ? 听起来很复杂,第一个问题:如果这些测试是真正的单元测试,为什么不将这些测试添加到project2中? You should test only units of your own code, not the code of another project. 您应该只测试自己代码的单元,而不测试另一个项目的代码。

Are you familiar with multi-module maven projects ? 您是否熟悉多模块Maven项目? It really helps organizing. 它确实有助于组织。

Why would you want to do that ? 你为什么想这么做 ? If you want to make sure that Project1 compiles before Project2, you must have Project2 depend on project1, and not the other way. 如果要确保Project1在Project2之前编译,则必须让Project2依赖于project1,而不是相反。

If you have cyclic dependencies like that, you have to asking the question if they really are two separate projects, or one project. 如果您具有这样的循环依赖关系,则必须问一个问题,它们是否确实是两个单独的项目,或者一个项目。 Often times, someone drew an arbitrary line and separated the two projects. 通常,有人划出任意界限,将两个项目分开。 If they are one project, then all of the tests will run all of the time. 如果它们是一个项目,则所有测试将一直运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM