scala 试图让 ScalaTest 工作:在做“mvn test”时“没有测试要运行”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13036561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:39:31  来源:igfitidea点击:

Trying to get ScalaTest working: "There are no tests to run" when doing "mvn test"

scalamavenautomated-tests

提问by Aaron Shaver

Edit: I finally got it to work!!!

编辑:我终于让它工作了!!!

It needed a combination of JUnit in the pom.xml, and three statements in my .scala:

它需要 pom.xml 中的 JUnit 和我的 .scala 中的三个语句的组合:

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])

For some reason, trying to configure Surefire makes the tests stop running again.

出于某种原因,尝试配置 Surefire 会使测试再次停止运行。



I'm coding in IntelliJ, trying to get a simple ScalaTest test running via a Maven (mvn test) build process. There aren't any errors, but unfortunately no tests run either.

我在 IntelliJ 中编码,试图通过 Maven ( mvn test) 构建过程运行一个简单的 ScalaTest 测试。没有任何错误,但不幸的是也没有运行测试。

Here is my .scala file:

这是我的 .scala 文件:

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter

class ExampleSuite extends FunSuite with BeforeAndAfter {

  before {
    println("Doing setup tasks...")
  }

  test("Example test of checking the browser title") {

    val expected_title = "Company Platform"
    var actual_title = "Company Platform"
    assert(actual_title == expected_title)
  }

  after {
    println("Doing teardown tasks...")
  }
}

Here's the Maven output:

这是 Maven 输出:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for JoeTestDemo:JoeTestDemo:jar:1.0
[WARNING] 'build.plugins.plugin.version' for org.scala-tools:maven-scala-plugin is missing. @ line 42, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building JoeTestDemo 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ JoeTestDemo ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ JoeTestDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-scala-plugin:2.15.2:compile (default) @ JoeTestDemo ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[WARNING] No source files found.
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ JoeTestDemo ---
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/Joeshaver/Projects/JoeTestDemo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ JoeTestDemo ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-scala-plugin:2.15.2:testCompile (default) @ JoeTestDemo ---
[INFO] Checking for multiple versions of scala
[INFO] includes = [**/*.scala,**/*.java,]
[INFO] excludes = []
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ JoeTestDemo ---
[INFO] Surefire report directory: /Users/Joeshaver/Projects/JoeTestDemo/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.368s
[INFO] Finished at: Tue Oct 23 10:58:30 PDT 2012
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------

Here is the pom.xml. I think my Maven Scala plugin might not be right:

这是 pom.xml。我认为我的 Maven Scala 插件可能不对:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>JoeTestDemo</groupId>
    <artifactId>JoeTestDemo</artifactId>
    <version>1.0</version>

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>scala-tools.org</id>
            <name>Scala-tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.9.0-1</version>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_2.9.0-1</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourceDir>src/main/java</sourceDir>
                    <jvmArgs>
                        <jvmArg>-Xms64m</jvmArg>
                        <jvmArg>-Xmx1024m</jvmArg>
                    </jvmArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

采纳答案by Blake Pettersson

If you're running on Maven, then you'll either have to annotate your test class(es) with

如果你在 Maven 上运行,那么你要么必须用

@RunWith(classOf[JUnitRunner])

This would require you having JUnit as a dependency. Or you could use the ScalaTest Maven Plugin(haven't used it myself though).

这将要求您将 JUnit 作为依赖项。或者您可以使用ScalaTest Maven 插件(虽然我自己没有使用过)。

回答by maba

You might want to use the scalatest-maven-pluginto run your scala tests.

您可能希望使用scalatest-maven-plugin来运行您的 Scala 测试。

Also make sure that you use the <scope>test</scope>for jars needed only for test purposes.

还要确保您使用<scope>test</scope>仅用于测试目的的罐子。

This is how I have defined my pom.xmlfor using ScalaTest:

这就是我定义我pom.xml使用ScalaTest 的方式

<?xml version="1.0" encoding="UTF-8"?>
<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.stackoverflow</groupId>
    <artifactId>Q13036561</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <scala.version>2.9.2</scala.version>
        <scalatest.version>2.0.M4</scalatest.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <id>scala-compile</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-make:transitive</arg>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <executions>
                    <execution>
                        <id>default-test</id>
                        < ! - Disable the default-test by putting it in phase none - >
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            -->
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0-M2</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <stdout>W</stdout> <!-- Skip coloring output -->
                </configuration>
                <executions>
                    <execution>
                        <id>scala-test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

As you can see I have also added a section that disables the surefire plugin if you really don't want to run it. It is commented in the above pom but if you want to disable the surefire plugin just uncomment that part.

如您所见,如果您真的不想运行它,我还添加了一个禁用surefire插件的部分。它在上面的 pom 中有注释,但如果你想禁用surefire插件,只需取消注释该部分。

I am using IntelliJ too and there are no problems at all with this combination. Just run the tests from within IntelliJ or by using mvn test, they will both work.

我也在使用 IntelliJ,这种组合完全没有问题。只需从 IntelliJ 内或使用 运行测试mvn test,它们都可以工作。

回答by Nikita Volkov

Adding to Blake's answer, besides @RunWith(classOf[JUnitRunner])you might also need to set the surefire as follows:

除了布莱克的回答之外,@RunWith(classOf[JUnitRunner])您可能还需要按如下方式设置万无一失:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
      <includes>
        <include>**/*Suite.class</include>
        <include>**/*Test.class</include>
        <include>**/*Tests.class</include>
        <include>**/*Spec.class</include>
        <include>**/*Specs.class</include>
      </includes>
    </configuration>
  </plugin>

Here's one of my Maven projects which tests just fine by ScalaTest without any special plugin: https://github.com/nikita-volkov/sext/

这是我的 Maven 项目之一,它在没有任何特殊插件的情况下通过 ScalaTest 测试得很好:https: //github.com/nikita-volkov/sext/