简体   繁体   English

如何将 JUnit 测试从一个 Maven 项目导入到另一个项目?

[英]How to import JUnit tests from one Maven project into another?

I have two Eclipse projects A and B that I use to run Selenium tests with JUnit.我有两个 Eclipse 项目 A 和 B,用于使用 JUnit 运行 Selenium 测试。

  • Project A tests a web app, so it contains tests that are divided into multiple packages each with their own test suite class (one package for each feature).项目 A 测试 Web 应用程序,因此它包含分为多个包的测试,每个包都有自己的测试套件类(每个功能一个包)。
  • Project B tests an application that uses A's web app but adds new features, so it contains its own tests but must also be able to reuse tests from A in its own test suite class when features are reused.项目 B 测试使用 A 的 Web 应用程序但添加了新功能的应用程序,因此它包含自己的测试,但还必须能够在重用功能时在其自己的测试套件类中重用来自 A 的测试。

In both projects, my test code is located under src/test/java , because I need to be able to test both separately.在这两个项目中,我的测试代码都位于src/test/java ,因为我需要能够分别测试两者。

The problem is that I can't reuse tests from A in B because I can't import the test packages from A .问题是我无法在B重用A中的测试,因为我无法从A导入测试包。 I tried to fix this by placing A 's packages in its src/main/java folder instead, but that means I can no longer run A 's test suites separately because it no longer has any tests to speak of.我试图通过将A的包放在其src/main/java文件夹中来解决这个问题,但这意味着我不能再单独运行A的测试套件,因为它不再有任何测试可言。

What I want to do is to simply have tests in my Project A, that I can execute, but to be able to reuse those test classes in Project B without duplicating code.我想要做的是在我的项目 A 中简单地进行测试,我可以执行这些测试,但能够在项目 B 中重用这些测试类而无需复制代码。 How can I do this?我怎样才能做到这一点?

You can package your test code in its own artifact您可以将测试代码打包到自己的工件中

 <!-- Packaging of test classes in JAR file -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

You can then import the dependency where you need it然后,您可以在需要的地方导入依赖项

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

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