11

編集:私はついにそれを機能させました!!!

pom.xmlのJUnitと、私の.scalaの3つのステートメントの組み合わせが必要でした。

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

@RunWith(classOf[JUnitRunner])

何らかの理由で、Surefireを構成しようとすると、テストの実行が再び停止します。


私はIntelliJでコーディングしており、Maven(mvn test)ビルドプロセスを介して単純なScalaTestテストを実行しようとしています。エラーはありませんが、残念ながらテストも実行されません。

これが私の.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...")
  }
}

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] ------------------------------------------------------------------------

これがpom.xmlです。私のMavenScalaプラグインは正しくないかもしれないと思います:

<?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>
4

3 に答える 3

14

を使用しscalatest-maven-pluginてscalaテストを実行することをお勧めします。

また<scope>test</scope>、テスト目的でのみ必要なjarにforを使用するようにしてください。

これは私がScalaTestpom.xmlを使用するために自分を定義した方法です:

<?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>

ご覧のとおり、本当に実行したくない場合は、surefireプラグインを無効にするセクションも追加しました。上記のpomでコメントされていますが、surefireプラグインを無効にする場合は、その部分のコメントを解除してください。

私もIntelliJを使用していますが、この組み合わせではまったく問題はありません。IntelliJ内から、またはを使用してテストを実行するだけで、mvn test両方とも機能します。

于 2012-10-24T07:13:44.240 に答える
9

Mavenで実行している場合は、テストクラスに注釈を付ける必要があります。

@RunWith(classOf[JUnitRunner])

これには、依存関係としてJUnitが必要になります。または、 ScalaTest Mavenプラグインを使用することもできます(ただし、自分で使用したことはありません)。

于 2012-10-23T18:18:15.577 に答える
7

ブレイクの答えに加え@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>

これが私のMavenプロジェクトの1つで、特別なプラグインなしでScalaTestによって問題なくテストされます:https ://github.com/nikita-volkov/sext/

于 2012-10-23T18:28:14.330 に答える