简体   繁体   English

在 Scala 项目中使用 Maven 运行集成测试和单元测试

[英]Run Integration test and Unit test using Maven in Scala project

In my Scala project I have two packages such as "src/test/scala/integration" and "src/test/scala/unit".在我的 Scala 项目中,我有两个包,例如“src/test/scala/integration”和“src/test/scala/unit”。 All the unit test and integration test are in their respective packages.所有的单元测试和集成测试都在各自的包中。 I want to execute them (either Unit tests or Integration tests) as per my need using commands like "mvn test".我想根据我的需要使用“mvn test”之类的命令来执行它们(单元测试或集成测试)。 How can this be achieved?如何做到这一点?

Here is my "pom.xml" file -这是我的“pom.xml”文件-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sarfaraz.demo</groupId>
  <artifactId>scala-project</artifactId>
  <version>0.1</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2019</inceptionYear>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.12.6</scala.version>
    <scala.compat.version>2.12</scala.compat.version>
    <failsafe.version>2.22.1</failsafe.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.48</version>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_${scala.compat.version}</artifactId>
      <version>3.0.5</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>2.27.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala/</testSourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.21.0</version>
      </plugin>

      <plugin>
        <groupId>org.scalatest</groupId>
        <artifactId>scalatest-maven-plugin</artifactId>
        <version>2.0.0</version>
        <configuration>
          <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
          <junitxml>.</junitxml>
          <filereports>TestSuiteReport.txt</filereports>
          <!-- Comma separated list of JUnit test class names to execute -->
          <jUnitClasses>StudentRepositoryTest</jUnitClasses>
        </configuration>
        <executions>
          <execution>
            <id>test</id>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>${failsafe.version}</version>
        <configuration>
          <includes>
            <include>**/*IT.scala</include>
          </includes>
        </configuration>
        <executions>
          <execution>
            <id>integration-tests</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
</project>

I my case, while using "mvn test", I am able to run the Unit tests.我的情况是,在使用“mvn test”时,我能够运行单元测试。

Here is the link to the GitHub repo: https://github.com/Sarfaraz214/ScalaDemo这是 GitHub 存储库的链接: https://github.com/Sarfaraz214/ScalaDemo

Any help will be much appreciated.任何帮助都感激不尽。 Thanks.谢谢。 :) :)

There are 2 issues here :这里有两个问题

First, given your integration and unit tests have separate source and resource directories.首先,鉴于您的集成和单元测试有单独的源和资源目录。 you need to add extra source and resource directories by using the Build Helper Maven Plugin.您需要使用 Build Helper Maven 插件添加额外的源和资源目录。

Second: Although you intended to separate your integration test using the configuration in fail-safe (**/*IT.scala) this would not work as intended in Scala as your tests are being run by your第二:虽然您打算使用故障安全 (**/*IT.scala) 中的配置来分离集成测试,但这在 Scala 中无法按预期工作,因为您的测试正在由您的

<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
</plugin>

I would answer how to resolve the first issue here and you could post another question on how to separate your integration tests and unit tests in Scala.我将在这里回答如何解决第一个问题,您可以发布另一个关于如何在 Scala 中分离集成测试和单元测试的问题。 (comment with a link to next question) (评论下一个问题的链接)

Resolution to first (This will allow you to run both unit and Integration test)第一个分辨率(这将允许您同时运行单元测试和集成测试)

You need to add this to your pom file and replace the directory tags with the appropriate paths to your resource and source folder您需要将此添加到您的 pom 文件中,并将目录标签替换为您的资源和源文件夹的适当路径

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>

      <execution>
        <id>add-integration-test-sources</id>
        <phase>generate-test-sources</phase>
        <goals>
          <goal>add-test-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>src/test/scala/integration</source>
          </sources>
        </configuration>
      </execution>

      <execution>
        <id>add-integration-test-resources</id>
        <phase>generate-test-resources</phase>
        <goals>
          <goal>add-test-resource</goal>
        </goals>
        <configuration>
          <resources>
            <resource>
              <directory>src/integration-test/resources</directory>
            </resource>
          </resources>
        </configuration>
      </execution>

    </executions>
  </plugin>

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

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