【问题标题】:Scalatest Maven Plugin "no tests were executed"Scalatest Maven 插件“未执行任何测试”
【发布时间】:2016-12-06 14:53:55
【问题描述】:

我正在尝试在 Maven 上使用 scalatestspark-testing-base 进行 Spark 集成测试。 Spark 作业读取 CSV 文件、验证结果并将数据插入数据库。我正在尝试通过放入已知格式的文件并查看它们是否以及如何失败来测试验证。这个特定的测试只是确保验证通过。不幸的是,scalatest 找不到我的测试。

相关pom插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <!-- enable scalatest -->
        <plugin>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                <wildcardSuites>com.cainc.data.etl.schema.proficiency</wildcardSuites>
            </configuration>
            <executions>
                <execution>
                    <id>test</id>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

这是测试类:

class ProficiencySchemaITest extends FlatSpec with Matchers with SharedSparkContext with BeforeAndAfter {
    private var schemaStrategy: SchemaStrategy = _
    private var dataReader: DataFrameReader = _

    before {
        val sqlContext = new SQLContext(sc)
        import sqlContext._
        import sqlContext.implicits._

        val dataInReader = sqlContext.read.format("com.databricks.spark.csv")
                                            .option("header", "true")
                                            .option("nullValue", "")
        schemaStrategy = SchemaStrategyChooser("dim_state_test_proficiency")
        dataReader = schemaStrategy.applySchema(dataInReader)
    }

    "Proficiency Validation" should "pass with the CSV file proficiency-valid.csv" in {
        val dataIn = dataReader.load("src/test/resources/proficiency-valid.csv")

        val valid: Try[DataFrame] = Try(schemaStrategy.validateCsv(dataIn))
        valid match {
            case Success(v) => ()
            case Failure(e) => fail("Validation failed on what should have been a clean file: ", e)
        }
    }
}

当我运行mvn test 时,它找不到任何测试并输出此消息:

[INFO] --- scalatest-maven-plugin:1.0:test (test) @ load-csv-into-db ---
[36mDiscovery starting.[0m
[36mDiscovery completed in 54 milliseconds.[0m
[36mRun starting. Expected test count is: 0[0m
[32mDiscoverySuite:[0m
[36mRun completed in 133 milliseconds.[0m
[36mTotal number of tests run: 0[0m
[36mSuites: completed 1, aborted 0[0m
[36mTests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0[0m
[33mNo tests were executed.[0m

更新
通过使用:

<suites>com.cainc.data.etl.schema.proficiency.ProficiencySchemaITest</suites>

代替:

<wildcardSuites>com.cainc.data.etl.schema.proficiency</wildcardSuites>

我可以让那个测试运行。显然,这并不理想。通配符套件可能已损坏;我要在 GitHub 上开一张票,看看会发生什么。

【问题讨论】:

  • ProficiencySchemaITest 在哪里?
  • src/test/scala/com/cainc/data/etl/schema/proficiency/ProficiencySchemaITest.scala。我的 pom 中有:src/main/scalasrc/test/scala
  • 我还可以在target/ 中看到.class 文件已创建。
  • 我有同样的问题没有使用wildcardSuites,所以问题可能更深。

标签: scala maven apache-spark scalatest


【解决方案1】:

这可能是因为项目路径中有一些空格字符。 删除项目路径中的空间,可以成功发现测试。 希望对您有所帮助。

【讨论】:

【解决方案2】:

尝试将 junit 排除为传递依赖项。为我工作。下面的示例,但请注意 Scala 和 Spark 版本特定于我的环境。

   <dependency>
        <groupId>com.holdenkarau</groupId>
        <artifactId>spark-testing-base_2.10</artifactId>
        <version>1.5.0_0.6.0</version>
        <scope>test</scope>
        <exclusions>
            <!-- junit is not compatible with scalatest -->
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusion>
    </dependency>

【讨论】:

    【解决方案3】:

    我遇到的测试没有被发现的问题归结为测试是从class 文件中发现的,所以为了让测试被发现,我需要将&lt;goal&gt;testCompile&lt;/goal&gt; 添加到scala-maven-plugin goals .

    【讨论】:

      【解决方案4】:

      对我来说,这是因为我没有使用以下插件:

      <plugin>
              <groupId>org.scala-tools</groupId>
              <artifactId>maven-scala-plugin</artifactId>
              <executions>
                <execution>
                  <goals>
                    <goal>compile</goal>
                    <goal>testCompile</goal>
                  </goals>
                </execution>
              </executions>
              <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
                <args>
                  <arg>-target:jvm-1.8</arg>
                </args>
              </configuration>
            </plugin>

      【讨论】:

        【解决方案5】:

        原因: Maven 插件不会在您运行 mvn 命令时编译您的测试代码。

        解决方法:

        使用您的 IDE 运行 scala 测试,这将编译测试代码并将其保存在目标目录中。当你下次运行 mvn test 或任何内部触发 maven 测试周期的 maven 命令时,它应该运行 scala 测试

        【讨论】:

          猜你喜欢
          • 2016-01-20
          • 2011-06-06
          • 2020-03-18
          • 2015-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-08
          • 2017-09-25
          相关资源
          最近更新 更多